PDA

View Full Version : Creating dynamic model/store



TobyJustus
16 Apr 2012, 12:37 AM
I create a dynamic model with this code:


//dynamic model modelFactory = function (name, fields) {
console.log('Creating dynamic model and store');


//create dynamic columns
var modelFields = [];


Ext.Array.each(fields, function (name, index, resultfields) {


var this_modelField = {};
this_modelField['mapping'] = name.name;
this_modelField['name'] = name.name;
modelFields.push(this_modelField);
});
//define names
var modelName = name;
var storeName = name;


//create model
Ext.define(modelName, {
extend: 'Ext.data.Model',
fields: modelFields
});
//create store
ND.store = new Ext.data.Store({




model: modelName,
sortOnLoad: true,
proxy: {
type: 'ajax',
url: '../json/result.json',
reader: {
type: 'json'
}
},
autoLoad: false


});


console.log('Dynamic model and store created');
}

This is working only the issue is that i always have the same store: ND.store. How can i create a dynamic store and use it in a grid?

vietits
16 Apr 2012, 1:17 AM
Instead of assigning store to ND.store, you should assign it to a local variable and then return it to the calling.


//dynamic model
modelFactory = function (name, fields) {
console.log('Creating dynamic model and store');


//create dynamic columns
var modelFields = [];


Ext.Array.each(fields, function (name, index, resultfields) {
var this_modelField = {};
this_modelField['mapping'] = name.name;
this_modelField['name'] = name.name;
modelFields.push(this_modelField);
});
//define names
var modelName = name;
var storeName = name;


//create model
Ext.define(modelName, {
extend: 'Ext.data.Model',
fields: modelFields
});
//create store
// ND.store = new Ext.data.Store({
var store = Ext.create('Ext.data.Store', {
model: modelName,
sortOnLoad: true,
proxy: {
type: 'ajax',
url: '../json/result.json',
reader: {
type: 'json'
}
},
autoLoad: false
});
console.log('Dynamic model and store created');
return store;
}

TobyJustus
16 Apr 2012, 1:30 AM
Thanks,

i use this in the function to create the grid:


modelFactory(rec.get('Reference'), rec.get('ResultFields'));

var grid = Ext.create('Ext.grid.Panel', {

title: 'Gridview',
forceFit: true,
store: store,
columns: tabColumns
})


But how will i know what the store name is? Now it will say store is not defined

TobyJustus
16 Apr 2012, 4:32 AM
i have the store working:


var store = Ext.create('Ext.data.Store', { model: modelName,
id: storeID,
sortOnLoad: true,
proxy: {
type: 'ajax',
url: '../json/result.json',
reader: {
type: 'json'
}
},
autoLoad: false


});
return store;

to load the function i do this:

var tabs = Ext.getCmp('northPanel'); var rec = view.getStore().getAt(rowIndex);
var store = rec.get('Reference');
modelFactory(rec.get('Reference'), rec.get('ResultFields'), store);

but now when i want to load or refresh a store i did this:


store.load({
url: '../json/result.json?'+form.getValues(true),

});

but then i get the error store.load is not a function

so i tried this:

rec.get('Reference').load({

url: '../json/result.json',


});

now i get rec.get('Reference').load is not a function.

How can i load a store?

vietits
16 Apr 2012, 5:18 AM
Thanks,

i use this in the function to create the grid:



modelFactory(rec.get('Reference'), rec.get('ResultFields'));
var grid = Ext.create('Ext.grid.Panel', {
title: 'Gridview',
forceFit: true,
store: store,
columns: tabColumns
})


But how will i know what the store name is? Now it will say store is not defined


var store = modelFactory(rec.get('Reference'), rec.get('ResultFields'));
var grid = Ext.create('Ext.grid.Panel', {
title: 'Gridview',
forceFit: true,
store: store,
columns: tabColumns
});

TobyJustus
16 Apr 2012, 5:22 AM
Thanks! This is doing the trick!