PDA

View Full Version : [FIXED]Model root is undefined error



sg707
1 Mar 2011, 8:16 AM
Ext.regModel('User', {
fields : [
{
name : 'id',
type : 'int'
}, {
name : 'name',
type : 'string'
}
],
hasMany : 'Post',
proxy : {
type : 'rest',
url : 'users',
format : 'json',
reader : {
type : 'json',
root : 'users'
}
}

});

Ext.regModel('Post', {
fields : [
{
name : 'title',
type : 'string'
}
],
belongsTo : 'User',

proxy : {
type : 'rest',
url : 'posts',
format : 'json',
reader : {
type : 'json' ,
root : 'posts'
}
}

});

User.load('1', {
success : function(record, operations) {
console.log(record);
});

As you can see, I'm creating Association Models w/ corresponding Proxy... however it's complaining that Post's root element is undefined. So if I comment out


root : 'posts'Then it works but this creates new problem...

If I were to use Post.load method... it doesn't know what "root" is therefore the records does not get parse. Same thing for store



var postStore = new Ext.data.Store( {
id : 'postStore',
model : 'Post'
});

sg707
1 Mar 2011, 8:19 AM
Error


root is undefined
total = root.length,

evant
2 Mar 2011, 4:57 AM
I'm not really clear on what you're saying, what does the data look like that's returned from your calls?

sg707
2 Mar 2011, 6:52 AM
{

-
users: [

-
{

-
posts: [

-
{

title: "post 0"

}
-
{

title: "post 1"

}
-
{

title: "post 2"

}

]
name: "John Lee"
id: 1

}

]

}


Ignore the special character above as copy/paste from json viewer. Let me try to rephrase




Ext.regModel('User', {
fields : [
{
name : 'id',
type : 'int'
}, {
name : 'name',
type : 'string'
}
],
hasMany : 'Post',
proxy : {
type : 'rest',
url : 'users',
format : 'json',
reader : {
type : 'json',
root : 'users'
}
}

});



Above code has many relationship w/ the Post Model



Ext.regModel('Post', {
fields : [
{
name : 'title',
type : 'string'
}
],
belongsTo : 'User'

});


With this the output from above is processed correctly. However, when I put proxy on "Post" model as below



Ext.regModel('Post', {
fields : [
{
name : 'title',
type : 'string'
}
],
belongsTo : 'User',
proxy : {
type : 'rest',
url : 'posts',
format : 'json',
reader : {
type : 'json',
root : 'posts'
}
}

});


Then it fails saying that Post.proxy.reader.root is "undefined" when I clearly defined it as



root : 'posts'


My guess is that User Model's "hasMany:Post" overrides the Post's root value. That's my wild guess.

sg707
2 Mar 2011, 7:04 AM
Here is my entire code





Ext.regModel('User', {
fields : [
{
name : 'id',
type : 'int'
}, {
name : 'name',
type : 'string'
}
],
hasMany : 'Post',
proxy : {
type : 'rest',
url : 'users',
format : 'json',
reader : {
type : 'json',
root : 'users'
}
}

});

Ext.regModel('Post', {
fields : [
{
name : 'title',
type : 'string'
}
],
belongsTo : 'User',
proxy : {
type : 'rest',
url : 'posts',
format : 'json',
reader : {
type : 'json',
root : 'posts'
}
}

});

var userStore = new Ext.data.Store( {
storeId : 'dataStore',
model : 'User'
});

var postStore = new Ext.data.Store( {
storeId : 'postStore',
model : 'Post'
});

Ext.onReady(function() {
var User = Ext.ModelMgr.getModel('User');


User.load('1', {
success : function (record, operations) {
console.log('This Will Not Print!');
console.log(arguments);
}
});

});


Run the above code w/ the same output from "users/1.json"

Condor
2 Mar 2011, 7:47 AM
You could solve this with:

Ext.regModel('User', {
fields: [{
name: 'id',
type: 'int'
}, {
name: 'name',
type: 'string'
}],
hasMany: 'Post',
proxy: {
type: 'rest',
url: 'users',
format: 'json',
reader: {
type: 'json',
root: 'users',
getAssociatedDataRoot: function(data, associationName) {
if (associationName == 'posts') {
return data; // don't return data['posts'], because the reader 'root' is already set to 'posts'
}
return this.callOverridden(arguments);
}
}
}
});

But it would prefer a better way to configure which reader should be used for associations (e.g. by using a getAssociatedReader method).

sg707
2 Mar 2011, 10:41 AM
Bow to the genius! =D>=D>=D>

That worked brilliantly. So, I guess the way I did it is "technically correct" but I'm ok w/ this workaround.

evant
15 Mar 2011, 5:14 AM
Ext.require('*');

Ext.onReady(function(){
Ext.regModel('User', {
fields: [{
name: 'id',
type: 'int'
}, {
name: 'name',
type: 'string'
}],
hasMany: {
model: 'Post',
reader: {
type: 'json'
}
},
proxy: {
type: 'ajax',
url: 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
});

Ext.regModel('Post', {
fields: [{
name: 'title',
type: 'string'
}],
belongsTo: 'User',
proxy: {
type: 'ajax',
url: 'posts.json',
reader: {
type: 'json',
root: 'posts'
}
}

});

var userStore = new Ext.data.Store({
storeId: 'dataStore',
model: 'User'
});

var postStore = new Ext.data.Store({
storeId: 'postStore',
model: 'Post'
});

var User = Ext.ModelMgr.getModel('User');


User.load('1', {
success: function(record, operations){
console.log(record.posts().getCount());
}
});


});




{
"users": [
{
"posts": [
{
"title": "post 0"
},
{
"title": "post 1"
},
{
"title": "post 2"
}
],
"name": "John Lee",
"id": 1
}
]
}


You can now specify a reader property on the associations, will be part of the next release.