PDA

View Full Version : How to get Model's Field value



irfaniqbal
24 Jan 2011, 5:54 AM
Hi all,
I have Loaded a store with Model 'User'. After loading, how can i get model's field values?

Thanks in advance.

babar.sajjad
24 Jan 2011, 6:44 AM
i m also trying this
var model = Ext.ModelMgr.getModel(ModelName);console.log(model) shows me some thing like this function (){ superclass.apply(this, arguments); } instead of model object :( , is this a right way to get model object?

mitchellsimoens
24 Jan 2011, 7:20 AM
Check out the Docs and read what each item does especially the 'get' method.

babar.sajjad
24 Jan 2011, 7:26 AM
thanks mitchell for your quick response
getModel returns the Ext.data.Model for a given model name but i am getting this function (){ superclass.apply(this, arguments); } when i call getModel function with model name as an argument. where mitchell do you think i am doing wrong?

mitchellsimoens
24 Jan 2011, 7:37 AM
Just doing sudo code...


Ext.ModelMgr.getModel(ModelName).get(FieldName);

You only have to worry when you get 'undefined' and 'null' for the most part.

babar.sajjad
24 Jan 2011, 7:40 AM
thanks i tried this before but i get Uncaught TypeError: Object function (){ superclass.apply(this, arguments); } has no method 'get', i am not sure where i am doing wrong :(

mitchellsimoens
24 Jan 2011, 7:42 AM
Are you doing this in an event of something or just by itself somewhere in the JS code?

babar.sajjad
24 Jan 2011, 7:44 AM
i am doing this in an event the code is like this


// loading store
var itemStore = Ext.StoreMgr.get('ItemStore');
// defining list items
var list = new Ext.List({
fullscreen: true,
itemTpl : [
'<div class="roundedBorder">',
'<div>',
'<div class="homeList">',
'<div class="{logoCls} fltL">{module}</div>',
'<div class="arrowRt fltR"></div>',
'</div>',

'</div>',
'<div class="pl10 homeMainListDesc">{description}</div>',
'</div>'
],
singleSelect: true,
allowDeselect: false,
indexBar: true,
store: itemStore,
listeners: {
itemtap: function (view, index, item, e){
showBackButton();
var rec = view.store.getAt(index);
if(rec.get('module') == 'Food'){
loadNutrition();
//app.setActiveItem(2);
}else if(rec.get('module') == 'Supplement'){
app.setActiveItem(3);
}
}
},
cls: 'homeMainList',
selectedItemCls: 'selectedListItem'
});
// rendering view
Application.views.home = Ext.extend(Ext.Panel, {
fullscreen: true,
layout: 'card',
items: [list]
});

// defining nutrition functionality and init the nutrition store
var loadNutrition = function(){
console.log(Ext.ModelMgr.getModel('User').get('id'));
}

mitchellsimoens
24 Jan 2011, 7:48 AM
Where is the User Model defined? Was it loaded?

Just a quick tip, in the itemtap listener for the List, instead of doing "rec.get('module')" twice, do something like:


var module = rec.get('module');
if (module === "Food") {
loadNutrition();
} else if (module === "supplement") {
app.setActiveItem(3);
}

babar.sajjad
24 Jan 2011, 8:02 AM
Thanks once again for your useful tip and reply.
Here is User Model.


Ext.ns('Model');
Model.User = Ext.regModel('User', {
idProperty: 'UserModel',
fields:[
'id', 'name'
]
}
});

Now i m loading User model like this:


var loadNutrition = function(){
Model.User.load();
console.log(Ext.ModelMgr.getModel('User').get('id'));
}

and above console.log give me following error
Object function (){ superclass.apply(this, arguments); } has no method 'get'

is this right approach?

mitchellsimoens
24 Jan 2011, 8:27 AM
Take a look at the docs http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.Model

Here is how you should load a Model:


//get a reference to the User model class
var User = Ext.ModelMgr.getModel('User');

//Uses the configured RestProxy to make a GET request to /users/123
User.load(123, {
success: function(user) {
console.log(user.getId()); //logs 123
}
});

So in the success function, you can do user.get("fieldname")

babar.sajjad
24 Jan 2011, 8:51 AM
Mitchell i already populated my model and do not want to make a request to server, my concern is that when a model is initialized once is there any way to get model object so that i can use its getter and setter functions to get values, as far as documentation is concerned there is a function getModel(modelname) but i do not know why it is not returning me the model object instead of function (){ superclass.apply(this, arguments); } why this function is returning me with this string, i am too much confused :(

mitchellsimoens
24 Jan 2011, 8:54 AM
Ok... so when you load, create a reference to that returned model...


success: function(user) {
this.userModel = user;
}

Now it is "attache" to whatever "this" is scoped to. Down the line you can use the get method.

babar.sajjad
24 Jan 2011, 9:19 AM
thanks Mitchell for your precious time and its very helpful for me, i have a question for you which i think can put me out of this problem , i have a user model and i used it in Store which makes a request to a server and loads the store, when doing this the models get loaded as well ? if not then how could i load both Store and model in a single ajax request.

Again thanks for your help :)

mitchellsimoens
24 Jan 2011, 9:22 AM
So you are using a Store? Well that's different.

To get a model instance, you need to find the model within the Store. You can use many things on the Store: findBy, findExact, find, findRecord, getAt, getById. Docs are good at examples for those.

However, if you are only loading 1 Model, use a Model with a Proxy. Stores are used if you want to handle multiple Model instances.

babar.sajjad
24 Jan 2011, 9:31 AM
yahooooo :) thanks Mitchell i got it and its only because of your help, sorry i could't ask you very clearly about my problem.

mitchellsimoens
24 Jan 2011, 9:33 AM
haha no problem :-)

ulubaht
1 Jun 2011, 6:30 AM
hi

u can get values in models method like load.



var yourModel=Ext.ModelManager.getModel('wuu.model.yourModel');
yourModel.load(yourIDorNull,
{
success:function(yourModel)
{alert(yourModel.get('YourField'));}
}
);