stefan_k
5 Dec 2010, 8:28 AM
Hi guys,
I'm trying to get my head around the whole Model-Store-Proxy-Reader thing. Even though I am able to work with a single model in my application, I am not able to work with different models and different stores. Maybe someone can push me into the right direction.
Here is what I do. First, I register a new application:
new Ext.Application({
name: 'CP',
launch: function() { ... }
});
Everything is done in the launch method. After that, I register two models:
CP.models.speaker = Ext.regModel('Speaker', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
],
associations: [
{ type: 'hasMany', model: 'Session', name: 'sessions' }
],
proxy: {
type: 'localstorage',
id: 'speakers'
}
});
CP.models.session = Ext.regModel('Session', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'speaker_id', type: 'int' },
{ name: 'title', type: 'string' }
],
associations: [
{ type: 'belongsTo', model: 'Speaker' }
],
proxy: {
type: 'localstorage',
id: 'sessions'
}
});
After that, I register two stores:
CP.stores.speaker = Ext.regStore('Speakers', {
model: 'Speaker',
data: speaker,
autoload: true,
proxy: {
type: 'memory',
reader: {
type: 'json',
}
}
});
CP.stores.session = Ext.regStore('Sessions', {
model: 'Session',
data: session,
autoload: true,
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});
I then create a new Ext.List that shows all the various speakers from the Speakers store:
var list = new Ext.List({
store: 'Speakers',
itemTpl: '{name}',
fullscreen: true
}, Ext.getBody());
So far so good, this works perfectly. What I want to do now is to listen for an item tapped in the list. After the tap, I want to access all sessions that are associated with this speaker. Therefore, I add this to the list config:
listeners: {
itemtap: function(view, index, item, e) {
var record = view.getStore().getAt(index);
record.sessions().load({
callback: function(records, operation) {
console.log(records);
}
}
}
}
Unfortunately, this prints out an empty array instead of the associated sessions. What am I missing? Any help is greatly appreciated, I have been trying to get this to work for the last couple of days. I tried it with Sencha Touch v1.0.0 and v1.0.1.
Here is some very simple dummy data that I used to test this:
var speaker = [
{ id: 1, name: 'John Doe' }
];
var session = [
{ id: 1, speaker_id: 1, title: 'My Session'}
];
Thank you
I'm trying to get my head around the whole Model-Store-Proxy-Reader thing. Even though I am able to work with a single model in my application, I am not able to work with different models and different stores. Maybe someone can push me into the right direction.
Here is what I do. First, I register a new application:
new Ext.Application({
name: 'CP',
launch: function() { ... }
});
Everything is done in the launch method. After that, I register two models:
CP.models.speaker = Ext.regModel('Speaker', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
],
associations: [
{ type: 'hasMany', model: 'Session', name: 'sessions' }
],
proxy: {
type: 'localstorage',
id: 'speakers'
}
});
CP.models.session = Ext.regModel('Session', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'speaker_id', type: 'int' },
{ name: 'title', type: 'string' }
],
associations: [
{ type: 'belongsTo', model: 'Speaker' }
],
proxy: {
type: 'localstorage',
id: 'sessions'
}
});
After that, I register two stores:
CP.stores.speaker = Ext.regStore('Speakers', {
model: 'Speaker',
data: speaker,
autoload: true,
proxy: {
type: 'memory',
reader: {
type: 'json',
}
}
});
CP.stores.session = Ext.regStore('Sessions', {
model: 'Session',
data: session,
autoload: true,
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});
I then create a new Ext.List that shows all the various speakers from the Speakers store:
var list = new Ext.List({
store: 'Speakers',
itemTpl: '{name}',
fullscreen: true
}, Ext.getBody());
So far so good, this works perfectly. What I want to do now is to listen for an item tapped in the list. After the tap, I want to access all sessions that are associated with this speaker. Therefore, I add this to the list config:
listeners: {
itemtap: function(view, index, item, e) {
var record = view.getStore().getAt(index);
record.sessions().load({
callback: function(records, operation) {
console.log(records);
}
}
}
}
Unfortunately, this prints out an empty array instead of the associated sessions. What am I missing? Any help is greatly appreciated, I have been trying to get this to work for the last couple of days. I tried it with Sencha Touch v1.0.0 and v1.0.1.
Here is some very simple dummy data that I used to test this:
var speaker = [
{ id: 1, name: 'John Doe' }
];
var session = [
{ id: 1, speaker_id: 1, title: 'My Session'}
];
Thank you