Francis Ducharme
Francis Ducharme

Reputation: 4987

Dynamic model manipulation

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.

Upvotes: 2

Views: 362

Answers (1)

Bojan Dević
Bojan Dević

Reputation: 1875

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.

Upvotes: 2

Related Questions