-
1 Mar 2011 8:16 AM #1
[FIXED]Model root is undefined error
[FIXED]Model root is undefined error
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 outCode: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); });
Then it works but this creates new problem...Code:root : 'posts'
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
Code:var postStore = new Ext.data.Store( { id : 'postStore', model : 'Post' });
-
1 Mar 2011 8:19 AM #2
Error
Code:root is undefined total = root.length,
-
2 Mar 2011 4:57 AM #3
I'm not really clear on what you're saying, what does the data look like that's returned from your calls?
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
2 Mar 2011 6:52 AM #4
Ignore the special character above as copy/paste from json viewer. Let me try to rephraseCode:{- -
users: [
- -
{
- -
posts: [
- -
{
- title: "post 0"
- -
{
- title: "post 1"
- -
{
- title: "post 2"
- -
{
- name: "John Lee"
- id: 1
- -
posts: [
- -
{
Above code has many relationship w/ the Post ModelCode: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' } } });
With this the output from above is processed correctly. However, when I put proxy on "Post" model as belowCode:Ext.regModel('Post', { fields : [ { name : 'title', type : 'string' } ], belongsTo : 'User' });
Then it fails saying that Post.proxy.reader.root is "undefined" when I clearly defined it asCode:Ext.regModel('Post', { fields : [ { name : 'title', type : 'string' } ], belongsTo : 'User', proxy : { type : 'rest', url : 'posts', format : 'json', reader : { type : 'json', root : 'posts' } } });
My guess is that User Model's "hasMany:Post" overrides the Post's root value. That's my wild guess.Code:root : 'posts'
- -
users: [
-
2 Mar 2011 7:04 AM #5
Here is my entire code
Run the above code w/ the same output from "users/1.json"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); } }); });
-
2 Mar 2011 7:47 AM #6Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
You could solve this with:
But it would prefer a better way to configure which reader should be used for associations (e.g. by using a getAssociatedReader method).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', 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); } } } });
-
2 Mar 2011 10:41 AM #7
Bow to the genius!



That worked brilliantly. So, I guess the way I did it is "technically correct" but I'm ok w/ this workaround.
-
15 Mar 2011 5:14 AM #8
Code: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()); } }); });You can now specify a reader property on the associations, will be part of the next release.Code:{ "users": [ { "posts": [ { "title": "post 0" }, { "title": "post 1" }, { "title": "post 2" } ], "name": "John Lee", "id": 1 } ] }Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
Thank you for reporting this bug. We will make it our priority to review this report.
Similar Threads
-
Showing error "root is undefined" where i am wrong?
By jaisonjames in forum Ext 3.x: Help & DiscussionReplies: 1Last Post: 6 Jan 2011, 12:57 AM -
[Solved]root is undefined - json error
By michaelc in forum Ext 2.x: Help & DiscussionReplies: 6Last Post: 24 Sep 2010, 2:09 AM -
[FIXED-187] The model data become undefined.
By taka_2 in forum Sencha Touch 1.x: BugsReplies: 5Last Post: 14 Aug 2010, 5:18 AM -
[Solved]Ext.data.JsonStore giving a "root is undefined" error.
By nickweavers in forum Ext 3.x: Help & DiscussionReplies: 3Last Post: 26 Dec 2009, 11:05 AM -
[FIXED][3.0-r4925] JsonReader readResponse root === undefined is moot
By mjlecomte in forum Ext 3.x: BugsReplies: 1Last Post: 29 Jul 2009, 7:09 PM


Reply With Quote