Sencha Touch 2 association problems - extjs

I get from server next JSON:
{
"contextPath":"http://apps.dhis2.org/demo",
"user":{
"id":"GOLswS44mh8",
"name":"System Administrator",
"isAdmin":true,
"ou":{
"id":"ImspTQPwCqd",
"name":"Sierra Leone"
}
}
}
I need convert this JSON in two Models : User model and OrganizationUnit model.
I read this tutorial http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.reader.Reader so it my code :
User model :
Ext.define('mobile-visualizer.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'},
{name: 'isAdmin', type: 'boolean'}
],
hasOne: {
model: "mobile-visualizer.model.OrganizationUnit",
name: "ou"
},
proxy: {
type: 'ajax',
url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
method : 'GET',
withCredentials : true,
useDefaultXhrHeader : false,
reader: {
type: 'json',
rootProperty: 'user'
}
}
}
});
OrganizationUnit model :
Ext.define('mobile-visualizer.model.OrganizationUnit', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'}
],
belongsTo: 'mobile-visualizer.model.User'
}
});
Store :
Ext.define('mobile-visualizer.store.UsersStore', {
extend : 'Ext.data.Store',
model : "mobile-visualizer.model.User",
autoLoad : false,
storeId : "usersStore",
proxy : {
type : 'ajax',
url : 'http://apps.dhis2.org/demo/dhis-web-visualizer/initialize.action',
method : 'GET',
withCredentials : true,
useDefaultXhrHeader : false,
reader : {
type : 'json',
rootProperty : 'user'
}
}
});
So, when I try get user from store using another code :
var store = Ext.create('mobile-visualizer.store.UsersStore');
store.load({
callback : function() {
// the user that was loaded
var user = store.first();
console.log(user.ou())
}
});
I have error : Uncaught TypeError: Object [object Object] has no method 'ou'
I can get all information about user but I can't get ou from user. It look like some association issue.
But I make all as like official tutorial . Please help to resolve this problem.
Thanks.

Follow my rules and everything will come out rosy:
http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html
http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html
These are for extjs, but very similar for sencha touch.
You are forgetting the associationKey.
Also, no need to redefine proxy in store as it will inherit its model's proxy.
Also, a user probably belongs to an org unit and doesn't have one...

Related

Extjs - Stop client to append any "id" with Model instance before sending it to server via REST

Client(Browser) is automatically appending "id" in my JSON which I am sending to server.
Here is my model with proxy:
fields: ['id','title','body'],
idProperty: 'id', // this is the default value (for clarity)
// clientIdProperty: 'cliendID',
identifier: 'sequential', // to generate -1, -2 etc on the client
proxy: {
type: 'rest',
//appendId: false,
limitParam:"",
filterParam: "",
startParam:'',
pageParam:'',
url:'http://localhost:3000/posts',
headers: {'Content-Type': "application/json" },
reader: {
type: 'json',
rootProperty:'posts'
},
writer: {
type: 'json'
}
}
When I create a model object to send data to server via Rest, Rest fill the 'id' field with (NameOfMymodel-number).
This is code to create and send model object to server via Rest:
var UserStore = Ext.getStore('peopleStore');
var user = Ext.create('ThemeApp.model.peopleModel',{'title': "Test", 'body': "Testing" });
user.save(); //POST /users
UserStore.load();
Is there any way to stop extjs from appending such id with my data?
This is a similar kind of problem but not what I am looking.
how do i prevent an extjs model/proxy from saving the empty primary id on create
Just set persist to false and that's it
Ext.define('ThemeApp.model.peopleModel', {
extend: 'Ext.data.Model',
fields: [ {name: 'id', type: 'int', persist: false},
{name: 'xyz', type: 'auto'}]
}
The 'idProperty' default value is 'id', so simply set persist: false for id property in your model.
credits: ajokon (Pointed this in comments by referring to another stackoverflow question)

Ext 4.2 Using proxy to save tree

when I use insertChild() or Sync(), proxy sends GET to server with just _dc param, what I should do to save my tree via proxy?
EDIT:added writer, now ext doing POST but with no data
write listener also didn't call
Ext.define('App.model.FileTree',
{
extend : 'Ext.data.Model',
fields : [
{name : 'id', type: 'string'},
{name : 'name', mapping:'name', type: 'string'},
{name : 'text', type: 'string'},
]
});
Ext.define('App.store.FileTree', {
extend: 'Ext.data.TreeStore',
alias:'filestore',
model : 'App.model.FileTree',
proxy: {
actionMethods: {
create: 'POST',
destroy: 'DELETE',
read: 'GET',
update: 'POST'
},
type: 'ajax',
url : '/app/store/FileTree.php',
reader: {
type: 'json'
},
writer: {
type: 'json',
nameProperty: 'mapping'
}
},
listeners : {
write: function(store, operation, opts){
Ext.each(operation.records, function(record){
if (record.dirty) {
record.commit();
}
});
}
}
});
trying add child like:
var tree = Ext.ComponentQuery.query('filetree')[0];
var record = tree.getSelectionModel().getSelection()[0];
record.appendChild({text:'test',name:'test',id:2,leaf:true});
tree.store.sync();
Configure a writer inside the proxy. Without the writer proxy does not know what to do.
I have a tree in Ext 5.0 (but it works also in Ext 4.x) with tree model configured this way:
Ext.define('At.model.ContinentModel',{
extend:'Ext.data.TreeModel'
,alias:'model.continentmodel'
,idProperty:'id'
,fields:[
{name:'text', type:'string', persist:true}
,{name:'iconColor', type:'string'}
]
,proxy:{
type:'ajax'
,url:'resources/service.php/tree/read/1/'
,writer:{
type:'json'
,allowSingle:false
,encode:true
,rootProperty:'data'
}
}
});
The tree store is configured with autoSync:true. Changes in text field trigger server requests that look like this:

Store cannot read xml from external source but can do it when reading from local source

I need to read content from a data source which is located on a remote server(I do not have access to modify anything)
I have tried days to get the content , but doesn't work.
What then I did was I downloaded this data source which is a xml file and put it under same folder with my code to test the correctness of my code syntax and found that the code works.
But when I changed back to the external data resource(
from: url: 'app/store/configuration.xml'
to : url: 'http://webtrak.bksv.com/mel/configuration'
), it still reads but returns no content.
This is not caused by CORS issue as I am testing my app on real devices.
Here are my store and model. Please help
Ext.define('myApp.store.SensorStationStore', {
extend: 'Ext.data.Store',
requires: ['myApp.model.SensorStation', 'Ext.data.reader.Xml'],
config:{
model: 'myApp.model.SensorStation',
storeId: 'SensorStore',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'http://webtrak.bksv.com/mel/configuration',
//url: 'app/store/configuration.xml',
reader: {
type: 'xml',
record: 'locations',
rootProperty: 'nmts'
}
}
}
});
Ext.define('myApp.model.SensorStation', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'name',
type: 'string',
mapping: '#name'
//convert: function (value, record) {
// Ext.Msg.alert(value,record.raw);
// //var nodes = rec.raw.querySelectorAll('');
//}
},
{
name: 'lat',
mapping: '#latitude',
type: 'float'
},
{
name: 'lng',
mapping: '#longitude',
type: 'float'
},
{
name: 'locid',
mapping:'#locid',
type: 'string'
}
]
}
});
Thank you.
I figured out what's the problem is... I have never worked with XML so,I don't know how the response of ajax request look like ,but by applying following code for store will fill your app's store(just a little change in your code)
Code:
Ext.define('myApp.store.SensorStationStore', {
extend: 'Ext.data.Store',
requires: ['myApp.model.SensorStation', 'Ext.data.reader.Xml'],
config:{
model: 'myApp.model.SensorStation',
storeId: 'SensorStore',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'http://webtrak.bksv.com/mel/configuration',
//url: 'app/store/configuration.xml',
reader: {
type: 'xml',
record: 'locations',
rootProperty: 'nmts'
}
}
} });
You are trying to apply store configs outside of config object. Cheers!!

Is id of model can only be int in extjs4?

I have a model and its id is type string. I load a record to display in grid but the id is show 'NaN'.
json
{"results":[{"id":"FT01","name":"area1","enable":true}],"total":1,"success":true}
model
Ext.define('YX.model.Area', {
extend : 'Ext.data.Model',
fields : [
{ name : 'id', type : 'string' },
{ name : 'name', type : 'string' },
{ name : 'enable', type : 'boolean', defaultValue : true }
]
});
store
Ext.define('YX.store.AreaStore', {
extend : 'YX.store.ListStore',
model : 'YX.model.Area',
proxy : {
type : 'ajax',
url : 'area/list.do',
reader : Utils.ajax.gridReader
}
});
Utils.ajax.gridReader =
{
type : 'json',
root : 'results',
successProperty : 'success',
totalProperty : 'total'
}
If I set proxy.reader to a direct object then id is show correctly in grid.
proxy : {
type : 'ajax',
url : 'area/list.do',
reader : {
type : 'json',
root : 'results',
successProperty : 'success',
totalProperty : 'total'
}
}
That may depends on what you do with the model, and the type of storage that backs your data. In principle, there should not be such a restriction, but that may be an expectation of some part of the framework (or third-party code).
Anyway, I've written a minimal test case with your model and a grid, and the id string is displayed correctly. That means that you probably have an issue somewhere else in your code... Most probably the proxy, reader or store.
Here's the test code (running there):
Ext.define('Area', {
extend: 'Ext.data.Model',
fields: [{
name: 'id',
type: 'string'
}, {
name: 'name',
type: 'string'
}, {
name: 'enable',
type: 'boolean',
defaultValue: true
}]
});
Ext.onReady(function() {
Ext.widget('grid', {
renderTo: Ext.getBody(),
height: 300,
columns: [
{dataIndex: 'id', text: "ID"},
{dataIndex: 'name', text: "Name"}
],
store: {
model: 'Area',
proxy: {
type: 'memory'
,data: [
{id: 'foo', name: "Foo"}
,{id: 'bar', name: "Bar"}
]
},
autoLoad: true
}
});
});
this error is because of your field name is "id".
default idProperty='id' field and idProperty field must have int data.but in your case it is not.
so this code is giving you this error.
if you won't need idProperty then chanege you field's name "id" to something else
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Model-cfg-idProperty
If you want your ID to be a number, you can customize it using identifier.
This is how it worked for me. I added this code in my model:
identifier: {
type: 'sequential',
seed: 10
},

ExtJS GridPanel "show in groups" not displayed/ not working

I encountered some weird behavior on GridPanel feature of ExtJs. I have included a groupField config option and it is not displayed and as well not working. Maybe you guys can give me an idea why?
createStore : function(itemPath) {
return new
CQ.Ext.data.GroupingStore({
proxy : new CQ.Ext.data.HttpProxy(
{
url : "/bin/test/private/folder/check.json",
method : "GET"
}),
//method: "GET",
reader: new CQ.Ext.data.JsonReader({
root: 'variables',
fields: [
{name: 'group', type: 'string'},
{name: 'path', type: 'string'},
{name: 'status', type: 'string'}
]
}),
updateData : function() {
// request the data
this.load({
params : {
path : itemPath
}
});
},
sortInfo: {field: 'path', direction:'ASC'},
groupField: 'group',
groupOnSort: true,
autoLoad : true
});
},
If you want your groups to be displayed in the grid panel, you also need to add the grouping feature to your grid.
Where is updateData coming from? I am not seeing it in the documentation anywhere. Is that piece of code causing the issue?
Here is the example from Sencha: http://dev.sencha.com/deploy/ext-3.4.0/examples/grid/grouping.html
Make sure you are viewing the 3.4 documentation too.

Resources