PDA

View Full Version : Problem retrieving model object



zachHurt
22 Jun 2012, 6:45 AM
Hello,
Every time I try to access my model object I get this:
"function () { return this.constructor.apply(this, arguments); } "

I just want the model object so I can access the fields of that model and change my grid columns accordingly. Here is the code I am currently using:

var grid = this.getGrid(),store = Ext.getStore('CarDataStore'),
model = Ext.ModelManager.get('MyApp.model.CarDataModel');
console.log(store);
var fields = model.getFields(),
cols=[];
console.log(fields);
// Create columns for new store
Ext.Array.forEach(fields, function (f) {
cols.push({
header: f.name,
dataIndex: f.name
});
});



grid.reconfigure(store, cols);

scottmartin
23 Jun 2012, 2:29 PM
Do you include your model in your controller?



models : [ 'User' ],

..

var model = Ext.ModelManager.getModel('App.model.User');
console.log(model);
model.getFields();



Regards,
Scott.

zachHurt
25 Jun 2012, 5:07 AM
Hey Scott,

I added the suggestion you had about adding the model in the controller and I am getting the same error.
I have my code for the controller shown below:
Ext.define('MyApp.controller.MyController', { extend: 'Ext.app.Controller',


models: [
'CarDataModel'
],
views: [
'MainPanel'
],


refs: [
{
ref: 'MainPanel',
selector: '#mainPanel'
},
{
ref: 'grid',
selector: '#grid'
},
{
ref: 'CarDataStore',
selector: '#carDataStore'
}
],


onButtonClick: function(button, e, options) {
var grid = this.getGrid(),
store = Ext.getStore('CarDataStore'),
model = Ext.ModelManager.getModel('MyApp.model.CarDataModel');
console.log(model);
console.log(store);
var fields = model.getFields();
cols=[];
console.log(fields);
// Create columns for new store
Ext.Array.forEach(fields, function (f) {
cols.push({
header: f.name,
dataIndex: f.name
});
});


grid.reconfigure(store, cols);
},


init: function() {
this.control({
"button": {
click: this.onButtonClick
}
});


}


});

I am really new to all this and dynamically changing a grid is a key feature to a proof of concept I am building.
Thanks.

scottmartin
27 Jun 2012, 7:22 AM
Can you show your model define?

You should be able to access the model:



var model = Ext.ModelManager.getModel('APP.model.User');
var model = APP.model.User;


Regards,
Scott.

zachHurt
27 Jun 2012, 7:26 AM
Here is how the model is defined. I am using architect and it just auto generated the code. I probably messed something simple up.


Ext.define('MyApp.model.CarDataModel', { extend: 'Ext.data.Model',


idProperty: 'CarDataModel',


fields: [
{
name: 'manufacturer',
type: 'string'
},
{
name: 'model',
type: 'string'
},
{
name: 'price',
type: 'int'
},
{
name: 'wiki',
type: 'string'
},
{
name: 'img',
type: 'string'
},
{
name: 'quality'
}
]
});

scottmartin
27 Jun 2012, 7:42 AM
My fault .. getModel only returns the definition, not an instance of the model

You can use:



var model = Ext.ModelManager.getModel('APP.model.User');
var model = APP.model.User;

console.log(model.prototype) // model.prototype.fields


Scott

zachHurt
27 Jun 2012, 7:55 AM
Awesome! That is giving me the model. I am just hung up on one last error.
I am trying to change the columns of the grid so I can make my grid "dyanmic" and I keep getting this error: Uncaught TypeError: Object [object Object] has no method 'forEach'




var grid = this.getGrid(),store = Ext.getStore('CarDataStore'),
model = Ext.ModelManager.getModel('MyApp.model.CarDataModel');
console.log(model.prototype);
console.log(store);
var fields = model.prototype.fields;
cols=[];
console.log(fields);
// Create columns for new store
Ext.Array.forEach(fields, function (f) {
cols.push({
header: f.keys,
dataIndex: f.keys
});
});


grid.reconfigure(store, cols);

scottmartin
27 Jun 2012, 8:48 AM
I would recommend a loop to be a bit faster ...




var fields = dmodel.prototype.field,
keys = fields.keys;

cols=[];

for (i = 0; i < keys.length; i++){
cols.push({
header: keys[i],
dataIndex: keys[i]
});
}

console.log(cols);


Scott

zachHurt
27 Jun 2012, 10:09 AM
Excellent it is all working, thank you so much for your help!

Tioecomp
6 Nov 2012, 6:39 AM
Just a Dumb question, how can I get the value of the field, I was having the same problem of zachHurt to get the Model, your solution scottmartin worked. But I dont know how to get the value of the field.