PDA

View Full Version : Use data from associated model; parent reads from nested json file



RoelKramer
19 Jul 2011, 3:58 AM
I'm loading json data in my associated models.



Ext.regModel('Festival', {
fields: [
{name: 'name', type: 'string'},
{name: 'id', type: 'int'}
],
hasMany: {model: 'Location', name: 'locations'},
proxy: {
//type: 'rest',
type: 'scripttag',
//url : 'festival_json_new.json',
url : 'http://127.0.0.1:8000/api/v1/festival/set/1/?format=jsonp',
reader: {
type: 'json',
root: 'objects'
}
}
});

Ext.regModel('Location', {
fields: [
{name: 'name', type: 'string'},
{name: 'festival_id', type: 'int'},
{name: 'id', type: 'int'}
],
belongsTo: 'Festival'

})


Festival has a proxy and url defined, so it's loaded into the festival model. The location model is associated with Festival.

Now, somewhere else in the code, i'd like to use only the Location model. But this doesn't work:

var locationStore = new Ext.data.Store({
model: "Location"
})

locationStore.load({
callback: function() {
console.log('** locationstore loaded**')
}
})

It has no server url specified, but I don't want to load the data from the server again. I already loaded some nested json data into festival model and would like to use that. How do I do this?

Parsing the Festival model + location works fine.


var festivalStore = new Ext.data.Store({
model: "Festival"
})

festivalStore.load({
callback: function() {
console.log('** festival complete **')
festivalStore.each(function(festival){
console.log(festival.get('name'))
console.log('festival:'+ festival.get('name'));
festival.locations().each(function(loc) {
console.log('festival location: '+ loc.get('name'));
})
});



}
});


Thanks.