Results 1 to 4 of 4

Thread: Model And Store

  1. #1
    Sencha User
    Join Date
    Aug 2012
    Location
    Bangalore
    Posts
    10

    Default Model And Store

    Hi,

    I am new to ExtJs.

    I want to know why do we use models and store? or rather when to use which one?
    Basic difference between the two?

  2. #2
    Sencha Premium User Tim Toady's Avatar
    Join Date
    Feb 2010
    Location
    Pennsylvania
    Posts
    576
    Answers
    67

    Default

    A model defines what your data looks like. For instance if you wanted to represent an employee, you would have fields defined for it such as name, salary, birthdate, address, etc. A store handles a collection of models. So perhaps you have a grid that lists all the employees in a company. That grid would have a store that contains all the employee model instances. The store lets you search through them, filter them, and many other things.

  3. #3
    Sencha User
    Join Date
    Aug 2012
    Location
    Dublin
    Posts
    72

    Default

    A model defines your data, and your objects (a class defines the semantics of a hierarchy of objects that shares the same behavior and properties).

    A store defines how do you store and manage the data, and in the case of ExtJS4 it's a layer between your backend where your real data is and the frontend.

    Example (using code made by the user scottmartin):

    Code:
     
    Ext.create('Ext.data.Store', {
            storeId : 'simpsonsStore',
            fields  : ['name', 'email', 'change', 'type'],
            data    : {'items' : [
                { 'name' : 'Lisa',  'email' : '[email protected]',  'change' : 100, 'type' : 'child' },
                { 'name' : 'Bart',  'email' : '[email protected]',  'change' : -20, 'type' : 'child'  },
                { 'name' : 'Homer', 'email' : '[email protected]',  'change' : 23 , 'type' : 'parent'  },
                { 'name' : 'Marge', 'email' : '[email protected]', 'change' : -11, 'type' : 'parent'  }
            ]},
            groupField: 'type',
            proxy   : {
                type   : 'memory',
                reader : {
                    type : 'json',
                    root : 'items'
                }
            }
        });
    With the store we can map it against your model (or you can map the model to a store):

    Code:
    Ext.define('MyApp.model.ModelTheSimpsons', {
        extend: 'Ext.data.Model',
    
        fields: [
            {
                name: 'name'
            },
            {
                name: 'email'
            },
            {
                name: 'change'
            },
            {
                name: 'type'
            }
        ]
    });
    If you put the data inline (in the store) you don't need the model because the fields are explicit. But you can use model in your store and you do not need to specify the fields.

    Then you can use a grid that is binded to that store, and it will display the data:

    Code:
    var grid = Ext.create('Ext.grid.Panel', {
            title      : 'Simpsons',
            store      : Ext.data.StoreManager.lookup('simpsonsStore'),
            columns    : [
                { header : 'Name', dataIndex : 'name' },
                { header : 'Email', dataIndex : 'email', flex : 1 },
                { header : 'Change', dataIndex : 'change', tdCls : 'x-change-cell' }
            ],
            features: [{
                ftype: 'groupingsummary'
            }]
        });
    You can put methods in your models so you can do additional logic from controllers,etc.

    That allows you to separate your logic and manipulate the data easily.

    Regards

    Quote Originally Posted by rupanpaul View Post
    Hi,

    I am new to ExtJs.

    I want to know why do we use models and store? or rather when to use which one?
    Basic difference between the two?

  4. #4
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,410
    Answers
    716

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •