PDA

View Full Version : hasMany associattion error



jodyford
17 Apr 2013, 4:46 AM
All,
I'm having issues with a jsonstore. I'm following this example:http://jsfiddle.net/alexrom7/4Zs5H/1/

I'm getting an error when I reference the hasMany model 'Submission":
--[08:41:50.575] TypeError: data is undefined @ http://localhost:57376/ext-4.2.0.663/ext-all-debug.js:5712

If I remove the SubMissions_Model, I do get the one record I expect. Normally, I'm loading this from a generic handler. that works correctly but I have created a variable mydata for this example. (the handler does not produce a 'data:..." entry.

What am I doing wrong?

here's my code


Ext.Loader.setConfig({ enabled: true });
Ext.Loader.setPath('Ext.ux.DataView', 'ux/DataView');
Ext.require([
'Ext.data.*',
'Ext.util.*',
'Ext.view.View',
'Ext.ux.DataView.Animated',
'Ext.XTemplate',
'Ext.panel.Panel',
'Ext.toolbar.*',
'Ext.slider.Multi'
]);

Ext.onReady(function () {
//data to be loaded into the ArrayStore

var mydata = {
data: {
"Mission_ID": 1, "Mission_Name": "Mission 1", "Submissions": [
{
"Submission_ID": 1, "Submission_Name": "Submission 1"
}, {
"Submission_ID": 2, "Submission_Name": "Submission 2"
}]
}
}

Ext.define('Mission_Model', {
extend: 'Ext.data.Model',
idProperty: 'Mission_ID',
//root: 'd',
fields: [
{ name: 'Mission_ID', mapping: 'Mission_ID', type: 'int' },
{ name: 'Mission_Name', mapping: 'Mission_Name', type: 'string' }],
hasMany: { model: 'Submission_Model', name: 'Submissions' }
});

Ext.define('Submission_Model'), {
extend: 'Ext.data.Model',
belongsTo: 'Mission_Model',
fields: [
{ name: 'Submission_ID', mapping: 'Submission_ID', type: 'int' },
{ name: 'Submission_Name', mapping: 'Submission_Name', type: 'string' }]
};

Ext.create('Ext.data.Store', {
storeId: 'Missions_Store',
model: 'Mission_Model',
autoLoad: true,
data: mydata,
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'data'
}
}
});

//var Missions_Store = new Ext.data.JsonStore({
// autoLoad: true,
// method: 'POST',
// model: 'Mission_Model',
// id: 'Mission_ID',
// proxy: new Ext.data.HttpProxy({
// url: 'data/getmissions.ashx'//
// }),
// listeners: {
// load: function (t, records, options) {

// Missions_Store.data.each(function (rec) {
// console.dir(rec.data)
// })}}});

var dataview = Ext.create('Ext.view.View', {
deferInitialRefresh: false,
store: Missions_Store,
tpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<div><div class="base inner shadow shadow5">',
'<strong>{Mission_Name}</strong>',
'</div>',
'<tpl for="(Submissions)">',
'<div class="base inner shadow shadow3">',
'<strong>{Submission_ID}</strong>',
'</div>',
'</tpl></p>',
'</div>',
'</tpl>'
),

plugins: [
Ext.create('Ext.ux.DataView.Animated', {
duration: 1000,
idProperty: 'Mission_ID'
})
],
id: 'phones',

itemSelector: 'div.base',
overItemCls: 'phone-hover',
multiSelect: true,
autoScroll: true
});

jodyford
19 Apr 2013, 6:23 AM
is this thing on?

slemmon
19 Apr 2013, 9:12 AM
I'm not used to seeing the data consumed by associated models flat - it's always nested. Do you have control over the data from the source?

jodyford
22 Apr 2013, 4:05 PM
I can structure the data however is most convenient. I think the data provided above is nested. "Submissions" indicate child records. The final interface should be similar to the image below. The example above should produce the first two levels but I'd like to think you could carry to example out to an infinite number of levels and nodes. Any advice on how to build the models, json is appreciated.

http://www.sencha.com/forum/image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbMAAAD5CAIAAAAiO0xuAAAVMUlEQVR4nO2du28UVxuHv7+QqWioaFKlSEdD5QIkpIBEETlFKJAcISQjWZGlxMUaMMRBJkYYW2GdyAuO7fiq9Qp7tVqvrPmKsQ9nz8ycPXPbmdfzvHqKyJnLmfmd88x1h//5FEVR1HD9r+wGUBRFVa4wI0VRlFmYkaIoyqxYM7ZP+r+8+ff76bVvf/jDm2gAAMjl5v1XEz+/f/KitXXYTW/GhbW9G/delr4xAAC58+RFK40ZG+//K73pAADFMVKOphnbJ33OFgHganP9zvPW7kkCM06//lx6owEAiubu0w8JzHj36YfSWwwAUDTfPPw9gRlv3n9VeosBAMZAAjOW3lYAgPGAGQEATDA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IsCMQsGMUmGAiQAzCgUzSoUBJgLMKBTMKBUGmAgwo1Awo1QYYCLAjELBjFJhgIkAMwoFM0qFASYCzCgUzCgVBpgIMKNQMKNUGGAiwIxCSWDG63eel95cUMTFRkyVwt2MBFcpEpjx1qO3pTcXAm7cexkXGzFVB0tMBFdl7MGZZvzpt79LbzEE3H78Li42YqoOlpgIrsrYgzPNuPqpXXqLIeDXP7fjYiOm6mCJieCqjD0404y+7z+c+av0RsPIMxFiqgKJThgJrjqMDC7CjP3BOeGVy61Hb/ePe/bkiKl0XGIiuAriElyEGYNa/ufo9uN3PEobf2bTrz+7DzNiEhETwVUE9+BizUhRFFXbwowURVFmYUaKoiiz/g8Qedkr227kWQAAAABJRU5ErkJggg==

Thanks,

Jody

jodyford
24 Apr 2013, 6:32 AM
Any help would be appreciated, please.

slemmon
24 Apr 2013, 12:28 PM
If you are able to deliver down the data all at once you might nest it together. Below is an example setup of three connected models and a nested data example to show how the data would look as its loaded by the reader to the model (and its associated model stores). Each associated models' data is found as a valueset under a property of the owner model's dataset.

Example models


Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name'],
proxy: {
type: 'rest',
url: 'data/users',
reader: {
type: 'json',
root: 'users'
}
},


hasMany: 'Post' // shorthand for { model: 'Post', name: 'posts' }
});


Ext.define('Post', {
extend: 'Ext.data.Model',
fields: ['id', 'user_id', 'title', 'body'],


proxy: {
type: 'rest',
url: 'data/posts',
reader: {
type: 'json',
root: 'posts'
}
},
belongsTo: 'User',
hasMany: {
model: 'Comment',
name: 'comments'
}
});


Ext.define('Comment', {
extend: 'Ext.data.Model',
fields: ['id', 'post_id', 'name', 'message'],


belongsTo: 'Post'
});


Example data:


{
success: true,
users: [{
id: 1,
name: 'Ed',
age: 25,
gender: 'male',
posts: [{
id: 12,
title: 'All about data in Ext JS 4',
body: 'One areas that has seen the most improvement...',
comments: [{
id: 123,
name: 'S Jobs',
message: 'One more thing'
}
]
}
]
}
]
}


If you haven't seen the Data Package guide and its subsection on Associations you might also check it out:
http://docs.sencha.com/extjs/4.2.0/#!/guide/data

jodyford
25 Apr 2013, 10:00 AM
Thank you. I will apply this solution and report status.

jodyford
25 Apr 2013, 3:51 PM
I'm almost there. What have I done here that keeps me from seeing gchildren? I cant get to that third level. thanks again.

here's the fiddle:
http://jsfiddle.net/vdmzh/5/

and the code.


Ext.define('ParentModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'parentField',
type: 'string'
}],

hasMany: {
model: 'ChildModel',
name: 'children'
}
});

Ext.define('ChildModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'childField',
type: 'string'
}],
hasMany: {
model: 'gChildModel',
name: 'gchildren'
}
});

Ext.define('gChildModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'gchildField',
type: 'string'
}]
})

Ext.require('*');

Ext.onReady(function () {

var store = Ext.create('Ext.data.Store', {
model: 'ParentModel'
});

store.loadRawData([{
parentField: "parent1",
children: [{
childField: "child1"
}, {
childField: "child2"
}]
}, {
parentField: "parent2",
children: [{
childField: "child3"
}, {
childField: "child4",
gchildren: [{
gchildField: "gchild1"
}]
}]
}]);

Ext.create('Ext.DataView', {
renderTo: Ext.getBody(),
store: store,
itemTpl: ['<table border="1"><tr><td>{parentField}</td></tr><tr><td><table border=1><tr><tpl for="children"><td>{childField}</td></tpl></tr><tr><td><table border=1><tr><tr><tpl for="gchildren"><td>{gchildField}</td></tpl></tr></table></td></tr></table></td></tr></table>']
});

})

jodyford
25 Apr 2013, 5:57 PM
It's merely a matter of css tricks now...

http://jsfiddle.net/vdmzh/10/

jodyford
25 Apr 2013, 7:06 PM
Thanks!