Dynamic Models, Stores, and Views -- Best way to
NOTE: Author is new to EXT JS and is trying to use MVC in his projects
imagine i have a web service whose data model is not fixed. I would want to have my models dynamically created, from which i dynamically create stores and hence dynamic components whose data is in those stores.
Lets start by seeing a sample class definition of a model:
Ext.define('MNESIA.model.User', { extend: 'Ext.data.Model' });
In this model definition, i have left out the 'fields'
parameter in the config object. This is because whwnever i create an instance of a model of the type above, i want to dynamically give it its fields definition, in other words i can have many instances of this model yet all having different definition of their 'fields'
parameter.
From here i create a definition of the开发者_StackOverflow store, like this:
Ext.define('MNESIA.store.Users', { extend: 'Ext.data.Store', autoLoad: true } });
There, i have a store definition. I have left out the 'model'
parameter because i will attach it to every instance of this class dynamically. Infact, even the 'data'
and 'proxy'
settings are not mentioned as i would want to asign them during the instantiation of this store.
From there, i would want to have dynamic views, driven by dynamic stores. Here below i have a definition of a Grid
Ext.define('MNESIA.view.Grid' , { extend: 'Ext.grid.Panel', alias : 'widget.mygrid', width: 700, height: 500 });
I have left out the following parameters in the Grid specification: 'columns'
, 'store'
and 'title'
. This is because i intend to have many Grids created as instances of the specification above yet having dynamic stores, titles and column definitions.
Some where in my controller, i have some code that appears like this:
function() { var SomeBigConfig = connect2Server(); /* where: SomeBigConfig = [ {"model":[ {"fields": ["SurName","FirstName","OtherName"] } ] }, {"store":[ {"data": {"items":[ {"SurName":"Muzaaya","FirstName":"Joshua","OtherName":"Nsubuga"}, {"SurName":"Zziwa","FirstName":"Shamusudeen","OtherName":"Haji"}, ... ] } }, {"proxy": { "type": "memory", "reader": { "type": "json", "root": "items" } } } ] }, {"grid",[ {"title":"Some Dynamic Title From Web Service"}, {"columns": [{ "header": "SurName", "dataIndex": "SurName", "flex": 1 },{ "header": "FirstName", "dataIndex": "FirstName", "flex": 1 }, { "header": "OtherName", "dataIndex": "OtherName", "flex": 1 } ]} ] } ] */ var TheModel = Ext.create('MNESIA.model.User',{ fields: SomeBigConfig[0].model[0].fields }); var TheStore = Ext.create('MNESIA.store.Users',{ storeId: 'users', model: TheModel, data: SomeBigConfig[1].store[0].data, proxy: SomeBigConfig[1].store[1].proxy }); var grid = Ext.create('MNESIA.view.Grid',{ store: TheStore, title: SomeBigConfig[2].grid[0].title, columns: SomeBigConfig[2].grid[1].columns }); // From here i draw the grid anywhere on the, page say by grid.renderTo = Ext.getBody(); // end function }
Now this kind of dynamic creating of models, then stores, and then grids does result into memory wastage and so this would require the destroy methods of each component to be called each time we want to destroy that component.
Questions:
Qn 1: Does the MVC implementation of EXT JS 4 permit this ?
Qn 2: How would i gain the same functionality by using thextypes
of my new classes. Say for example:
{ xtype: 'mygrid', store: TheStore, title: SomeBigConfig[2].grid[0].title, columns: SomeBigConfig[2].grid[1].columns }
Qn 3: If what i have written above really works and is pragmatically correct, can i apply this to all components like Panels, TabPanels, Trees (whereby their Configs are sent by a remote server) ?
Qn 4: If i have Controllers A and B, with controller A having a specification of views: [C, D] and Controller B having views: [E, F], would it be correct if actions generated by view: E are handled by Controller A ? i.e. Can a controller handle actions of a view which is not registered in its views config ?
NOTE: am quite new to Ext JS but would love to learn more. Advise me on how to improve my EXT JS learning curve. Thanks
In my option ,your model must map onto the store that is to be rendered to the view like for example,if implement the model part like this
{"model":[{"fields":[{name:'name',type:'string'}, {name:'id',type:'string'}]}]},this will be easily mapped onto the store for the view render it.
精彩评论