PDA

View Full Version : Adding Association Models



mediahack
7 Aug 2013, 5:43 AM
I'm having problems getting associated models to register with parent models. I have a list that has a store and that store has a model with a hasMany child model. I update the main store then I update the child store, but when I find a record from the parent store, I am seeing an array of size zero, when I know there is data. Here is some sample code.

Home.js (this is my view)

{ title: 'Tables',
iconCls: 'list',
layout: 'fit',
items: [
{
xtype: 'list',
id: "tablesList",
alias: "tablesList",
itemTpl: '<img src="images/icons/{status}.png" width="20px" height="20px" /> {name} {Reservations[0].last_name}',
grouped: true,
height: "100%",
width: "100%",
store: "Tables",
}
]
}

Tables (store and model):


Ext.define('TC.store.Tables', {
extend: 'Ext.data.Store',
config: {
storeId: 'Tables',
model: 'TC.model.Table',
autoLoad: true,


grouper: {
groupFn: function(record) {
return record.get('room');
},
sortProperty: 'room'
},


proxy:{
type: "localstorage",
id: "table"
}
}
});

Ext.define('TC.model.Table',{
extend: 'Ext.data.Model',
config: {
reader: "json",
idProperty: "id",
identifier: 'sequential',
autoLoad: true,
fields: [
{ name: "id", type: "int" },
{ name: "seating_id",type: "int" },
{ name: "room_id", type: "int"},
{ name: "room", type: "string"},
{ name: "name", type: "string"},
{ name: "min", type: "string"},
{ name: "max", type: "string"},
{ name: "priority", type: "string"}
],

hasMany:[
{
model: 'TC.model.Reservation',
name: 'Reservations',
primaryKey: 'id',
foreignKey: 'seating_id',
foreignStore: 'Reservations'
}
],

proxy:{
type: "localstorage",
id: "table"
}
}
});



Reservation (store and model):

Ext.define('TC.store.Reservations',{ extend: 'Ext.data.Store',
config: {
storeId: 'Reservations',
model: 'TC.model.Reservation',
autoLoad: true,
proxy:{
type: "localstorage",
reader: {
type: "json"
}
}
}
});

Ext.define('TC.model.Reservation',{
extend: 'Ext.data.Model',

config: {
idProperty: 'id',
identifier: 'sequential',
autoLoad: true,
fields: [
{ name: "id"},
{ name: "reservation_id", type: "int"},
{ name: "status", type: "string"},
{ name: "first_name", type: "string"},
{ name: "last_name", type: "string"},
],
belongsTo: [{
model: "TC.model.Table",
name: "Table",
primaryKey: "reservation_id",
foreignKey: 'seating_id',
foreignStore: 'Tables'
}
],

proxy:{
type: "localstorage",
reader:{
type: 'json',
nameProperty: 'mapping'
}
}
}
});


when I pull down the reservation JSON I check to see if its a specific status. Then I add it to the reservation store:


resStore.addData(temp);
resStore.sync();

var self = this;
self.getTablesList().refresh();


This doesn't work. I am not seeing the reservations and the list itemTpl doesn't seem to be showing the data. Help please.

fmoseley
9 Aug 2013, 2:08 PM
What version of Sencha Touch are you using?

mediahack
9 Aug 2013, 2:09 PM
ST 2.2.1