PDA

View Full Version : Using 'requires' or 'uses' in models/stores/grids?



hermann.s
28 Oct 2013, 6:03 AM
Greetings,

How can I define a Model, Store and Grid that use the 'uses' or 'requires' property?

Model

Ext.define('MCS.model.myTask', {
extend: 'Ext.data.Model',
fields: [
{name: 'Case_ID', type : 'Auto'},
{name: 'Project', type : 'Auto'},
{name: 'Task', type : 'Auto'},
{name: 'Title', type : 'Auto'}
],
});


Store

Ext.define('MCS.store.myTask', {
extend: 'Ext.data.Store',
model: 'MCS.model.myTask',
singleton: true,

proxy:
{
type: 'ajax',
url: 'Task/GetMyTaskData',
reader:
{
type: 'json',
root: 'data'
},
},
pageSize: 10,
autoLoad: false
});
});

Grid

Ext.define('MCS.view.gridpanel.myTask', {
extend: 'Ext.grid.Panel',
alias: 'widget.myTask',

itemId: 'myTask',
title: 'My Tasks',

collapsible: true,
flex: 1,
hideMode: 'offsets',
margin: '5 5 0 5',

store: //what goes here?
columns: [],

initComponent: function ()
{
this.columns =
[
{ text: 'Case ID', dataIndex: 'Case_ID' },
{ text: 'Project', dataIndex: 'Project' },
{ text: 'Task', dataIndex: 'Task' },
{ text: 'Title', dataIndex: 'Title' },
];

this.callParent();
}
});

Tim Toady
28 Oct 2013, 8:15 AM
requires and uses are specified just like any other configuration property. From the docs:


Ext.define('Mother', {
uses: ['Child'],
giveBirth: function () {
// This code might, or might not work:
// return new Child();

// Instead use Ext.create() to load the class at the spot if not loaded already:
return Ext.create('Child');
}
})


You will want to specify the full namespace. There are some instances where the Ext already knows requirements. For instance, extend.

It looks like there is some confusion with the use of a store with MVC in your post. In general, you want to specify your stores in your controller or app.js with the stores config. See http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.app.Controller-cfg-stores (http://docs.sencha.com/extjs/4.2.2/#%21/api/Ext.app.Controller-cfg-stores)
Your store doesn't need to be singleton. Ext will automatically create the store for you and you can use the storeId (or classname if left unspecified as I generally do) in your grid.

hermann.s
28 Oct 2013, 9:45 AM
Thanks Tim,

so, would I have to completely remove any store references from my GridPanel code?
How would that work? Any examples?

Tim Toady
28 Oct 2013, 10:12 AM
In general, you would only have one reference to your store within the view for your grid: the store config. In the controller that handles the grid, you can use the getStore method or a another getter to get the store instance when you need it. I generally only break from this when I need to create multiple instances, but seeing that you are using a singleton, I don't think that is the case for you. If you haven't read the App Architecture guide (parts 2 and 3 specifically), you should look into it. It answers many of the questions you have.
http://docs.sencha.com/extjs/4.2.2/#!/guide/mvc_pt2
(http://docs.sencha.com/extjs/4.2.2/#!/guide/mvc_pt2)http://docs.sencha.com/extjs/4.2.2/#!/guide/mvc_pt3
(http://docs.sencha.com/extjs/4.2.2/#!/guide/mvc_pt2)