Dynamic model manipulation

2019-08-13 07:45发布

I was Googling around for best practices regarding model manipulation, and apparently, in 4.x, you had this function (setField, example here).

But in 6.x, this seems to be gone. I remember reading on the Sencha forums that dynamic models aren't really 'best practices' anyways, so is this why it seems gone in v6 ?

I can do it on the prototype

MyModel.prototype.fields.push(Ext.create('Ext.data.field.Field', { ... }));

But is this the best way of doing it ?

We're going to have grids where users can hide columns so sometimes, model validation will have to change. Also, user defined fields will either by numeric, date, string, etc. depending how what type they chose, so again validations will change dynamically.

Thanks.

1条回答
Luminary・发光体
2楼-- · 2019-08-13 08:26

You could try to dynamically define a model and then call store.setModel().

var starkStore = Ext.create('Ext.data.Store', {
    model: Ext.data.Model, // only here to suppress warning
});
var starkModel = Ext.define(Ext.getId(), {
    extend: 'Ext.data.Model',
    fields: ['id', 'first_name', 'last_name']
});

starkStore.setModel(starkModel);
starkStore.getProxy().getReader().setModel(starkModel);

starkStore.loadData([
    { id: 1, first_name: 'Rob', last_name: 'Stark' },
    { id: 2, first_name: 'John', last_name: 'Snow' },
    { id: 3, first_name: 'Rickon', last_name: 'Stark' },
    { id: 4, first_name: 'Bran', last_name: 'Stark' },
]);

Example on jsfiddle

The only issue here is that you need to have unique name for the dynamic model.

查看更多
登录 后发表回答