1. #1
    Sencha User
    Join Date
    Jan 2009
    Posts
    1
    Vote Rating
    0
    oldrichard is on a distinguished road

      0  

    Default How to get a model instance by an Ext.ModelMgr api?

    How to get a model instance by an Ext.ModelMgr api?


    I went thought the Extjs4 api and could not find the proper way to get an model instance by Ext.ModelMgr api other than Ext.ModelMgr.create(). There was a sample in the Blog

    Code:
    Ext.regModel('User', {
        fields: ['id', 'name', 'age'],
        proxy: {
            type: 'rest',
            url : '/users',
            reader: {
                type: 'json',
                root: 'users'
            }
        }
    });
    
    var UserModel = Ext.getModel('User');
    var ed = new UserModel({
        name: 'Ed Spencer',
        age : 25
    });
     
    ed.save({
        success: function(ed) {
            console.log("Saved Ed! His ID is "+ ed.getId());
        }
    });
    I expected there would be a corresponding like this

    Code:
    var UserModel = Ext.getModel('User');
    var newUser = new UserModel({});
    
    newUser.load(123, {
        success: function(newUser) {
            console.log("Loaded user 123: " + newUser.get('name'));
        }
    });
    I was not able to make this work. Anybody know what's wrong with that?

  2. #2
    Sencha User
    Join Date
    Oct 2009
    Location
    Bucharest, Romania
    Posts
    70
    Vote Rating
    0
    icflorescu is on a distinguished road

      0  

    Default


    The code you're looking for is this:
    Code:
        var UserModel = Ext.ModelMgr.getModel('User');
        
        UserModel.load(123, {
            success: function(newUser) {
                console.log("Loaded user 123: " + newUser.get('name'));
            }
        });
    If you have previously registered a User model the above code works fine.

    There's a catch, however. Assuming you're testing like everybody with a json file, 123.json must provide an array with a single user, otherwise it won't work.
    Which I find a bit strange, there's no point of expecting an array when all you're trying to do is loading a single model instance. But let's be patient, maybe this is one of the things that are going to be revised during the beta stage...

    So 123.json must be something like this (note the square brakets):
    Code:
    [
        {
            "id":       1,
            "name":  "John Doe",
            "email":  "jdoe@example.com"
        }
    ]
    See more in this short thread.

  3. #3
    Sencha User
    Join Date
    Oct 2009
    Location
    Bucharest, Romania
    Posts
    70
    Vote Rating
    0
    icflorescu is on a distinguished road

      0  

    Default


    There's something else I have noticed and others may benefit from knowing it, you can register your namespaced model like this:
    Code:
        Ext.regModel('App.Data.User', {
            fields: [ 'id', 'name', 'email' ]
            proxy: {
                type:      'rest',
                url:        'data/users',
                format:   'json'
            }
        });
    And then use it like this:
    Code:
    App.Data.User.load(1, { ... });
    The necessary namespace definition is automatically created for you; also note there's no need to declare a model var when you need to use load or create.

Similar Threads

  1. [FIXED-114] Creating new instance of Model results in "dirty" object
    By jeroenvduffelen in forum Sencha Touch 1.x: Bugs
    Replies: 8
    Last Post: 23 Feb 2011, 6:05 PM
  2. Load record from LocalStorage using Ext.ModelMgr
    By edrmp in forum Sencha Touch 1.x: Discussion
    Replies: 3
    Last Post: 15 Sep 2010, 10:46 AM
  3. [CLOSED] Model instance get's parent Store as object after inserting
    By jeroenvduffelen in forum Sencha Touch 1.x: Bugs
    Replies: 1
    Last Post: 20 Jul 2010, 7:23 AM
  4. Transfer widget values to model instance - conceptual question
    By TheBerliner in forum Ext 3.x: Help & Discussion
    Replies: 15
    Last Post: 21 Nov 2009, 10:51 PM

Thread Participants: 1

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi