Results 1 to 7 of 7

Thread: Single Model Instance

  1. #1
    Ext Premium User
    Join Date
    Aug 2012
    Posts
    8
    Vote Rating
    0
      0  

    Default Single Model Instance

    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:
    Code:
     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

  2. #2
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347
    Vote Rating
    119
      1  

    Default

    1. You should include singleton: true config to your class definition.
    2. To access this singleton, just use App.model.Cart.
    Code:
    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)
    });

  3. #3
    Ext Premium User
    Join Date
    Aug 2012
    Posts
    8
    Vote Rating
    0
      0  

    Default

    thx, that was it - i did't know that models as well as stores areable to be singleton...

  4. #4
    Ext Premium User
    Join Date
    Aug 2012
    Posts
    8
    Vote Rating
    0
      0  

    Default

    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
    Code:
    App.model.Cart.proxy.read(...)
    but i'm not quite sure. As well i tried
    Code:
    autoLoad: true
    which has no effect at all...
    any idea?

    thx

  5. #5
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347
    Vote Rating
    119
      0  

    Default

    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:
    Code:
    App.model.Cart.proxy.read(
        Ext.create('Ext.data.Operation', {
            action: 'read', 
            id: 0,  // id of record to load 
            records: [App.model.Cart]
        })
    );

  6. #6
    Ext Premium User
    Join Date
    Aug 2012
    Posts
    8
    Vote Rating
    0
      0  

    Default

    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?

  7. #7
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347
    Vote Rating
    119
      0  

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •