-
16 Apr 2012 12:37 AM #1
Answered: Creating dynamic model/store
Answered: Creating dynamic model/store
I create a dynamic model with this code:
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?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'); }
-
Best Answer Posted by vietits
-
16 Apr 2012 1:17 AM #2
Instead of assigning store to ND.store, you should assign it to a local variable and then return it to the calling.
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({ 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; }
-
16 Apr 2012 1:30 AM #3
Thanks,
i use this in the function to create the grid:
But how will i know what the store name is? Now it will say store is not definedCode:modelFactory(rec.get('Reference'), rec.get('ResultFields')); var grid = Ext.create('Ext.grid.Panel', { title: 'Gridview', forceFit: true, store: store, columns: tabColumns })
-
16 Apr 2012 4:32 AM #4
i have the store working:
to load the function i do this:Code: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;
but now when i want to load or refresh a store i did this:Code: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 then i get the error store.load is not a functionCode:store.load({ url: '../json/result.json?'+form.getValues(true), });
so i tried this:
now i get rec.get('Reference').load is not a function.Code:rec.get('Reference').load({ url: '../json/result.json', });
How can i load a store?
-
16 Apr 2012 5:18 AM #5
-
16 Apr 2012 5:22 AM #6
Thanks! This is doing the trick!


Reply With Quote