View Full Version : Load grid from model without store?

8 Dec 2011, 6:29 AM
Hello guys,

For some reason I'd like to control CRUD actions for my objects by myself using, for example manual Ext.data.JsonP.requests.

So, let's assume I have User model and some method that returns valid json:

Ext.define('ikhtml.model.User', {
extend: 'Ext.data.Model',
fields: ['name', 'login', 'password']

// pseudo-code
collectionOfModel = getUsers(); // how to translate json response into "Users"?
grid.setDataSource(collectionOfModel); // and how to do this for Ext.grid.panel?

Could you please advise how to implement these 2 pseudo-code lines in a lightweight and elegant way?

Thanks in advance.

8 Dec 2011, 8:28 AM
Create a store, see http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.Panel the inline data section.

There's a sample like below

Ext.create('Ext.data.Store', {
model: 'User',
data : [
{firstName: 'Ed', lastName: 'Spencer'},
{firstName: 'Tommy', lastName: 'Maintz'},
{firstName: 'Aaron', lastName: 'Conran'},
{firstName: 'Jamie', lastName: 'Avins'}

But that datapart you can do with your getUsers() function

var store = Ext.create('Ext.data.Store', {
model: 'User',
data : getUsers()



getUsers() should return an Array with User instances.

Due a bug it could be possible that you must also provide a column array in the grids reconfigure function. But it should work without.


grid.reconfigure(store, columnsconfig);

Other nice sample, without model: http://docs.sencha.com/ext-js/4-0/#!/example/grid/array-grid.html

9 Dec 2011, 2:15 AM
Thank you, it works. Just one detail I still don't understand:

Ext.require('ikhtml.model.User'); // it works only if I add this line to module.

var usersStore = Ext.create('Ext.data.Store', {
model: 'ikhtml.model.User',
data: this.users

I there a way of auto-registering model?
Also I see that in "Simple MVC Example" model isn't explicitly required in other modules and it still works. Why? :)

9 Dec 2011, 2:22 AM
Ext.require('ikhtml.model.User'); is just loading it if it wasn't loaded.

If you put the model definition in a js file that is loaded before you use it, the you don't need require.

9 Dec 2011, 3:21 AM
But how is it loaded in Sencha's MVC example (extjs\examples\app\simple\)?

9 Dec 2011, 3:51 AM
With require :) but I do not use sencha's MVC structure. That why you did not found anything about it in my answer.

9 Dec 2011, 4:11 AM
I don't see require there, but nevermind. I'll just "blindly" use require. ))

Thank you!