PDA

View Full Version : Model And Store



rupanpaul
21 Sep 2012, 7:42 AM
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?

Tim Toady
21 Sep 2012, 8:03 AM
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.

iplanit
21 Sep 2012, 9:33 AM
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):



Ext.create('Ext.data.Store', {
storeId : 'simpsonsStore',
fields : ['name', 'email', 'change', 'type'],
data : {'items' : [
{ 'name' : 'Lisa', 'email' : 'lisa@simpsons.com', 'change' : 100, 'type' : 'child' },
{ 'name' : 'Bart', 'email' : 'bart@simpsons.com', 'change' : -20, 'type' : 'child' },
{ 'name' : 'Homer', 'email' : 'home@simpsons.com', 'change' : 23 , 'type' : 'parent' },
{ 'name' : 'Marge', 'email' : 'marge@simpsons.com', '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):



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:


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


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?

scottmartin
21 Sep 2012, 12:05 PM
And yet more info :D

http://www.sencha.com/forum/showthread.php?240808

Scott.