PDA

View Full Version : Single Model Instance



mariodavid
23 Aug 2012, 1:14 AM
hi,

i've a question about a single model instance. I would like to have a single instance (singleton) of a model and communicate this model directly without a store to a remote resource.

the model is a cart object, that exists just once in the app for every client. So i tried to do something like this:


Ext.define('App.model.Cart', {
extend : 'Ext.data.Model',
idProperty : 'id',
associations: [
{type: 'hasMany', model: 'App.model.Event', name: 'renting_events'},
{type: 'belongsTo', model: 'App.model.Customer', name: 'customer'},
{type: 'belongsTo', model: 'App.model.User', name: 'user'}
],
proxy : {
type : 'ajax',
url : '/cart.json'
},
});


but i don't really get the idea of how to access this object in my application. could someone give me a hint? perhaps i'm just searching for the wrong thing... :)

thanks, mario

vietits
23 Aug 2012, 1:30 AM
1. You should include singleton: true config to your class definition.
2. To access this singleton, just use App.model.Cart.


Ext.onReady(function(){
Ext.define('App.model.Cart', {
singleton: true,
extend : 'Ext.data.Model',
idProperty : 'id',
associations: [
{type: 'hasMany', model: 'App.model.Event', name: 'renting_events'},
{type: 'belongsTo', model: 'App.model.Customer', name: 'customer'},
{type: 'belongsTo', model: 'App.model.User', name: 'user'}
],
proxy : {
type : 'ajax',
url : '/cart.json'
}
});


console.log(App.model.Cart)
});

mariodavid
23 Aug 2012, 1:44 AM
thx, that was it - i did't know that models as well as stores areable to be singleton...

mariodavid
23 Aug 2012, 2:00 AM
i just forget something:
as you said, one can access the object by doing this: App.model.Cart, but how can this model load itself up from the given proxy url, so that when i access the Model, the correct values are in place?
I tried something like
App.model.Cart.proxy.read(...) but i'm not quite sure. As well i tried
autoLoad: true which has no effect at all...
any idea?

thx

vietits
23 Aug 2012, 5:32 AM
App.model.Cart is an model instance and it has all methods that a model has. As you see in API document, an instance of model has no method to load data from server. However, you can use the following trick to load data from server:


App.model.Cart.proxy.read(
Ext.create('Ext.data.Operation', {
action: 'read',
id: 0, // id of record to load
records: [App.model.Cart]
})
);

mariodavid
23 Aug 2012, 11:04 AM
hi,

ok, that was what i tried, although i didn't succeed. in my case i don't have an id to be given as an argument, because there is just "one" cart (that's why the url is /cart.json). The singleton stuff also doesn't seem to work with associations.
But my main point is: this doesn't seem to be the "right" way... i thought this use case with a single object instance is pretty common, isn't it? so how do you handle that kind of situations? the other possibility that seems obvious is do create a store and put that object as the only one in it?

vietits
23 Aug 2012, 3:09 PM
No. I don't use this kind of singleton model in my application. Instead, I define models, stores and create instances each time I need them.