PDA

View Full Version : How do i access records (models?) in my store?



dr-dan
11 Jul 2012, 4:50 AM
I am trying to get my head round what appears to be the simplest of things:


I want to load a collection of objects via a JSON-returning server-side call,


why do all these return something unexpected?


> modelStore.data.items[0].Id
undefined
> modelStore.data.items[0].getId()
undefined
> modelStore.data.items[0].getTitle()
TypeError: Object [object Object] has no method 'getTitle'
> modelStore.getById(1)
null
> modelStore.getTotalCount()
1
> modelStore.first()
Ext.Class.newClass < should be PegModel??


**The code:**



Ext.define('PegfileApp.model.PegModel', {

extend: 'Ext.data.Model',

fields: ['Id', 'Title'],

//hasMany: {model: 'RootDimension', name: 'rootDimensions'},

proxy:
{
type: 'ajax',
url: 'PegModel',
timeout: 120000,
noCache: false,
reader:
{
root: 'PegModel',
successProperty: 'success'
}
}
});


Ext.define('PegfileApp.store.PegModels', {
extend: 'Ext.data.Store',
model: 'PegfileApp.model.PegModel',
autoLoad: true,
autoSync: false,
proxy: {
type: 'ajax',
url: 'PegModel',
reader: {
type: 'json',
root: 'PegModels',
successProperty: 'success'
}
}
});


**The data:**



{"PegModels":[{"Title":"PegModel","Id":1}],"success":true}


I understood for each property defined on the Model, we have a corresponding getter: get{PropertyName}()?


Do i need to cast the "Record" somehow to a "Model"?

Ex_Soft
11 Jul 2012, 5:19 AM
> modelStore.getById(1)
null

Ext.data.Model.idProperty (http://docs.sencha.com/ext-js/4-1/#%21/api/Ext.data.Model-cfg-idProperty)


Ext.define('PegfileApp.model.PegModel', {
...
fields: [
{ name: 'Id', type: "int" },
'Title'
],
idProperty: "Id"
...
});

dr-dan
11 Jul 2012, 5:25 AM
Thanks for the tip on the Id :)

dr-dan
11 Jul 2012, 5:28 AM
I've been confused by learning too much stuff too quickly, and how most of the time, by defining some property, ExtJS creates a getter for it (e.g. controller refs, model associations)

this is not the case fot Model properties and hence my confusion

so i guess my question is why would you not create getters for model properties? :-/