-
18 Feb 2011 8:36 PM #1
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
I expected there would be a corresponding like thisCode: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 was not able to make this work. Anybody know what's wrong with that?Code:var UserModel = Ext.getModel('User'); var newUser = new UserModel({}); newUser.load(123, { success: function(newUser) { console.log("Loaded user 123: " + newUser.get('name')); } });
-
19 Feb 2011 6:19 AM #2
The code you're looking for is this:
If you have previously registered a User model the above code works fine.Code:var UserModel = Ext.ModelMgr.getModel('User'); UserModel.load(123, { success: function(newUser) { console.log("Loaded user 123: " + newUser.get('name')); } });
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):
See more in this short thread.Code:[ { "id": 1, "name": "John Doe", "email": "jdoe@example.com" } ]
-
19 Feb 2011 6:27 AM #3
There's something else I have noticed and others may benefit from knowing it, you can register your namespaced model like this:
And then use it like this:Code:Ext.regModel('App.Data.User', { fields: [ 'id', 'name', 'email' ] proxy: { type: 'rest', url: 'data/users', format: 'json' } });
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.Code:App.Data.User.load(1, { ... });
Similar Threads
-
[FIXED-114] Creating new instance of Model results in "dirty" object
By jeroenvduffelen in forum Sencha Touch 1.x: BugsReplies: 8Last Post: 23 Feb 2011, 6:05 PM -
Load record from LocalStorage using Ext.ModelMgr
By edrmp in forum Sencha Touch 1.x: DiscussionReplies: 3Last Post: 15 Sep 2010, 10:46 AM -
[CLOSED] Model instance get's parent Store as object after inserting
By jeroenvduffelen in forum Sencha Touch 1.x: BugsReplies: 1Last Post: 20 Jul 2010, 7:23 AM -
Transfer widget values to model instance - conceptual question
By TheBerliner in forum Ext 3.x: Help & DiscussionReplies: 15Last Post: 21 Nov 2009, 10:51 PM


Reply With Quote