what is extjs data store in root parameter - extjs

I don't have clear idea about extjs 'root' parameter in data store.
var xxx = new Ext.data.JsonStore
({
autoLoad: true,
url: 'www.dataserver.com',
root: 'data',
idProperty: 'ID',
fields: ['ID', 'Name']
});
i feel this very small thing but i'm very new to extjs please explain to me what is use for this root above data tore

It's a config of the JsonReader.
The JsonStore accepts all configs of JsonReader.
In the root config you give the name of the property that'll contain the array of row objects.
In your case it'll be :
{
data: [
{ID: 1, Name: "some name"},
{ID: 2, Name: "another name"}
]
}

Readers are used to interpret data to be loaded into a Model instance or a Store - often in response to an AJAX request. In general there is usually no need to create a Reader instance directly, since a Reader is almost always used together with a Proxy, and is configured using the Proxy's reader configuration property
Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
root: 'users'
}
},
});
The above reader is configured to consume a JSON string that looks something like this:
{
"success": true,
"users": [
{ "name": "User 1" },
{ "name": "User 2" }
]
}
See in the above json structure, we have two attribute(with key:value pair) as "success" amd "users. The "users" key has array of object, which we are going to use in store. so in this json we are going to use the "users" attribute for our reader, so only we mention "users" as root for that store.
As recap, root is the config which will tell which part of attribute/object im going to use for my store in consume json.
Thanks

Related

How do i modify a raw data object returned by an ExtJS AJAX proxy into a JSON object to be consumed by a Tree Store

In an effort to create a treepanel, i configure it with a treestore whose AJAX proxy url receives json data i have no control of. But using Ext.data.reader.Json's transform property invokable before readRecords executes, gives an option to modify the passed raw (deserialized) data object from the AJAX proxy into a modified or a completely new data object. The transform config, gives the code snippet below:
Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
transform: {
fn: function(data) {
// do some manipulation of the raw data object
return data;
},
scope: this
}
}
},
});
I would please like an example on how to go about modifying the return JSON object
[
{
"id": 3,
"attributes":
{},
"name": "user_one",
"login": "",
"email": "user_one#ats",
"phone": "0751223344",
"readonly": false,
"administrator": false,
"password": null
},
{
"id": 4,
"attributes":
{},
"name": "user_two",
"login": "",
"email": "user_two#ats",
"phone": "0751556677",
"readonly": false,
"administrator": false,
"password": null
}
]
into a JSON object fit for a treestore.
The hierarchical tree is to be rendered to show which user is under which admin using a condition administrator==true from the returned JSON, then a second AJAX request that returns that admin's users shown here.
[
{
"user_id": 3,
"admin_id": 1,
},
{
"user_id": 4,
"admin_id": 2,
}
]
Is the data nested at all? Otherwise why use a treepanel instead of a grid? To your question though, it'll depend on how you configure your treepanel but it would probably be something like this:
transform: {
fn: function(data) {
var treeRecords = Ext.Array.map(data, function(i){
return {
text: i.name,
leaf: true
//any other properties you want
}
});
var treeData = {
root: {
expanded: true,
children: treeRecords
}
};
return treeData;
},
scope: this
}

ExtJS 6 inherited hasMany-Relation nested loading

I try to setup a hasMany relationship and want load my main entity with all the associated models in a single request.
But that seems not to work with "hasMany" relations that is inherited.
I have a BaseModel that defines all relations and fields and a "normal" model that defines the proxy to load from.
These are my (relevant) models:
Ext.define('MyApp.model.BaseUser', {
"extend": "Ext.data.Model",
"uses": [
"MyApp.model.UserEmail",
"MyApp.model.Account"
],
"fields": [
{
"name": "name"
},
{
"name": "accountId",
"reference": {
"type": "MyApp.model.Account",
"role": "account",
"getterName": "getAccount",
"setterName": "setAccount",
"unique": true
}
}
]
"hasMany": [
{
"name": "emails",
"model": "MyApp.model.UserEmail"
}
],
});
Ext.define('MyApp.model.User', {
extend: "MyApp.model.BaseUser",
proxy: {
type: 'rest',
url : '/api/user',
reader: {
type: 'json',
rootProperty: 'data',
}
}
});
Ext.define('MyApp.model.UserEmail', {
extend: 'Ext.data.Model',
"fields": [
{
"name": "id",
"type": "int"
},
{
"name": "email",
"type": "string"
},
],
proxy: {
type: 'rest',
url : '/api/user/email',
reader: {
type: 'json',
rootProperty: 'data',
}
}
});
// MyApp.model.Account looks like MyApp.model.UserEmail
This is my server's response:
{
data: [
{
name: 'User Foo'
accountId: 50
account: {
id: 50,
balance: 0
},
emails: [
{
id: 70,
email: 'hello#world.de'
}
]
}
]
}
The "account" relation is working on the "normal" User Model and I can access it via the auto-generated method user.getAccount() as I expected.
Now I tried to access the users emails with the auto-generated methods:
// user is of 'MyApp.model.User'
user.emails(); // store
user.emails().first(); // null
user.emails().count(); // 0
It seems that the "emails"-relation models were not loaded into my user model. Am I accessing them the right way?
I can access them via user.data.emails. But this is an array of plain objects, not of UserEmail-Objects.
Can anyone give me some advice? Is nested loading supported with keyless associations?
Kind regards,
czed
Edit:
Clearified what I ment.
It should work. Here's a working fiddle. Check console for nested loaded data.

Interacting with legacy Has One associations in Extjs 5.1.1

I had to upgrade from 4.1.2 to 5.1.1 for the sole sake of widget columns. I'm having trouble getting hasone associations to work.
I've got a model that looks like this:
Ext.define('PP.model.LM.FOOModel', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.field.Field'
],
fields: [
{
name: 'ID'
},
//Boatload of simple fields
],
hasOne: {
model: 'PP.model.LM.FOO1Model',
name: 'FOO1',
associationKey: 'FOO1'
}
});
When I interact with the model, there is no getter \ setter methods, and FOO1Model's data is only present as an object that can be accessed by
record.get('FOO1');
Could someone please point out what exactly am I doing wrong?
I tried doing it with the new approach - creating a field with a reference to the desired model. It works fine when I call setFoo1, and then do a get. But.
When I make an Ajax request, and try reading received JSON using Ext.data.reader.Json, it seems to fail to understand that a certain property in the object is in fact an associated model. The data in Foo1 appears in the model as an object in Foo1 property. The reqest returns an array of models that have many FooModels.
The Json looks like this:
{
"root": [{
"ID": 4241,
"Foos": [{
ID: 2237,
"Foo1": {
"ID": 1216
}
}]
}],
"success": true
}
It seems that hasOne doesn't exist in ExtJS 5.1 it is now done like this:
fields: [{
name: 'addressId',
reference: 'Address',
unique: true
}]
Check Model api for more info.
Had to debug the Ext.data.reader.Json to understand. The key was passing associationKey in reference config. Unless it's specified the reader will assume that the data for the association resides under '_foo1' field in JSON.
Is it mentioned anywhere in sencha docs? I don't think so. Am I supposed to feel like an imbecile for not guessing that?
Sample for unfortunate poor sods like me, who might encounter the problem in future:
{
name: 'Foo1',
reference: {
type: 'FOO1Model',
association: 'Foo1',
associationKey: 'Foo1'
},
unique: true
}

Loading data from the json file into treepanel

In my data.json file I have static data which is to be loaded in the treepanel.According to this link reference, I have changed "children" to "data".But still I am not geting data in my tree panel grid.
My data.json file
{
"data":
[
{
"App": "active"
"iconCls":"task-folder",
"expanded":"false",
"data": [
{
"AppReport1": "active",
"leaf": "true"
}
]
}
]
}
Here Is what I am doing:
initComponent: function () {
var me = this;
Ext.log("initComponent()", me);
me.treeStore = Ext.create("Ext.data.TreeStore", {
proxy: {
type: "ajax",
model : 'EPH.model.DBStatusModel',
autoLoad: true,
type: 'ajax',
url:'app/data/data.json',
reader: {
type: 'json',
root : 'data'
}
},
root: {
expanded: true
}
});
this.down('treepanel').setRootNode(store.getRootNode());
}
I think I have problem in my data.json file.Is it correct or do I need to change something.Please help me out resolving this issue.Thanks.
In the reference link the first data appears to be JSON Object and in your JSON file is an JSON Array. I think this it should look.
{
"data": //root
{
"data"://children
[
{
"App": "active"
"iconCls":"task-folder",
"expanded":"false",
"data": [
{
"AppReport1": "active",
"leaf": "true"
}
]
}
]
}
}
Note: This code was taken from the original JSON file of the author.
Childen it's a important part of configuration for Node interface.
Sencha api: children
This config param say you what data root node has.
EDIT:
and what happend with "defaultRootProperty":
Sencha api: treeStore
For the tree to read nested data, the Ext.data.reader.Reader must be
configured with a root property, so the reader can find nested data
for each node (if a root is not specified, it will default to
'children'). This will tell the tree to look for any nested tree nodes
by the same keyword, i.e., 'children'. If a root is specified in the
config make sure that any nested nodes with children have the same
name. Note that setting defaultRootProperty accomplishes the same
thing.

Association is not working in extjs4 when I am uploading my application to server?

I am working in extjs4 MVC. I am getting stuck at a point where I have been working on association.Association is working properly when I am using in my system local side.But when I am uploading it to server side then the associated data is not displaying.I am trying a lot but not getting answer how this conflict occurs.
My application is working properly at client side.It displays associated data also.But when when I uploading data to server side then it does not display associated data.
Here is my application's some part which is working at local side:--
But when I am uploading my side to server side then the options means associated data is not displayed
Here Is my some code:--
1) Model class :--
Ext.define('B.model.kp.PollModel',{
extend: 'Ext.data.Model',
idproperty:'',//fields property first position pk.
fields: ['pollId','pollQuestion','isActive','pollTypeId','createDate','userId','publishDate','isPublished','approvalUserId','percentagevotes','optionId'],
hasMany:{
model:'B.model.kp.PolloptionModel',
foreignKey:'pollId',
name:'options',
},
proxy:
{
type:'ajax',
api:
{
read:'index.php/KnowledgePortal/Poll/GetPublicPoll',
create:'index.php/KnowledgePortal/Pollvote/setPollVote',
},//End of api
reader:
{
type:'json',
root: 'polls',
//successProperty: ,
},//End of reader
writer:
{
type:'json',
root: 'records',
//successProperty: ,
},
}//End of proxy
});
2) Store class :--
Ext.define('B.store.kp.PollStore',{
extend: 'Ext.data.Store',
model: 'B.model.kp.PollModel',
autoLoad: true,
});//End of store
3) View class:--
Ext.define('B.view.kp.poll.PollView',{
extend:'Ext.view.View',
id:'pollViewId',
alias:'widget.PollView',
store:'kp.PollStore',
config:
{
tpl:'<tpl for=".">'+
'<div id="main">'+
'</br>'+
'<b>Question :-</b> {pollQuestion}</br>'+
'<tpl for="options">'+
'<p>{#}.<input type="radio" name="{parent.pollId}" value="{optionId}">&nbsp{option}</p>'+
'</tpl></p>'+
'</div>'+
'</tpl>',
itemSelector:'div.main',
},
});// End of login class
4) here is my main view class
Ext.define('B.view.kp.poll.Poll',{
extend:'Ext.form.Panel',
requires:[
'B.view.kp.poll.PollView'
],
id:'pollId',
alias:'widget.Poll',
title:'Poll',
//height:180,
items:[
{
xtype:'PollView',
},
],//end of items square
buttons:[
{
disabled:true,
name:'vote',
formBind:true,
text:'vote',
action:'voteAction'
},
{
text:'result',
action:'resultAction',
}
]
});
5) And here is the json file --
{
'polls': [
{
"pollId": 1,
"pollQuestion": 'Who will win the match',
"options": [
{
"optionId":1,
"option":'India',
"pollId":1
},
{
"optionId": 2,
"option": 'Pakistan',
"pollId":1
},
{
"optionId": 3,
"option": 'Srilanka',
"pollId":1
},
{
"optionId": 4,
"option": 'Australia',
"pollId":1
},
],
}
]
}
6) PollOption model class :--
Ext.define('B.model.kp.PolloptionModel',{
extend: 'Ext.data.Model',
//idproperty:'',//fields property first position pk.
fields: ['optionId','option','pollId','percentagevotes'],
associations:[
{type:'HasMany', model:'Pollvotemodel', foreignKey:'optionId'},
{type:'BelongsTo', model:'PollModel', foreignKey:'pollId'},
]
});
Please someone give me some suggestion to solve this problem...
are you sure your server-side data feed is exactly the same as your local data feed.
Are the related questions entered into the live database?

Resources