jenson-button-event
jenson-button-event

Reputation: 18961

ExtJS 4 - please explain Model & Store

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"?

Upvotes: 1

Views: 7508

Answers (2)

user4462227
user4462227

Reputation: 11

Realize this is an old post but this still comes up a lot even today. To simplify, consider Model an object and Store an array. A Store is an array of objects. Sometimes you'll work with just an object. Other times you want to work with a collection of objects. Libraries such as Ext extend these "objects" and "arrays" with methods that allow you to operate on them more conveniently.

Upvotes: 1

sha
sha

Reputation: 17860

You're almost there. :)

Store has array of models but you don't access them directly via items[]. You use store.getAt(index) method instead if store.items[index] for example.

Also check out store.find() method and similar to it to get an idea how to get that index for particular record.

Once you got record which will be model instance you use record.get('fieldname') to get different fields.

Upvotes: 3

Related Questions