0

I'm defining a store and I want to dynamically assign a Model for it at creation. So if I create my DropDownStore and I don't pass a model config with it it needs to rely on the default model(DropDownModel).

Here is my DropDownModel + DropDownStore:

Ext.define('DropDownModel', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'Id', type: 'int' },
        { name: 'Name', type: 'string' }
    ]
});
Ext.define('DropDownStore', {
    extend: Ext.data.Store,
    proxy: {
        type: 'ajax',
        actionMethods: { read: 'POST' },
        reader: {
            type: 'json',
            root: 'data'
        }
    },
    constructor: function(config) {
        var me = this;

        if (config.listUrl) {
            me.proxy.url = config.listUrl;
        }

        me.model = (config.model) ? config.model : 'DropDownModel'; //This line creates some weird behaviour

        me.callParent();

        //If the URL is present, load() the store.
        if (me.proxy.url) {
            me.load();
        }
    }
});

This is a creation of the DropDownStore with a dynamic model:

Ext.define('RelationModel', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'Id', type: 'int' },
        { name: 'RelationName', type: 'string' },
        { name: 'RelationOppositeName', type: 'string' }
    ]
});

...//a random combobox
store: Ext.create('DropDownStore', {
    listUrl: 'someprivateurl',
    model: 'RelationModel'
})
...

When I edit the line in the constructor method to
me.model = (config.model) ? config.model : undefined
It works like expected for the dynamic model but not anymore for the default models.

If I let it be
me.model = (config.model) ? config.model : 'DropDownModel';
It works for the default models and not for the dynamic model.

How can I assign a model to a store at creation?

4

1 回答 1

1
constructor: function(config) {
    var me = this;

    if (config.listUrl) {
        me.proxy.url = config.listUrl;
    }
    me.callParent();

    if (config.extraFields) {
        me.model.setFields(config.extraFields);
    }

    //If the URL is present, load() the store.
    if (me.proxy.url) {
        me.load();
    }
}

store: Ext.create('DropDownStore', {
    listUrl: 'someprivateurl',
    extraFields: [
        { name: 'Id', type: 'int' },
        { name: 'RelationName', type: 'string' },
        { name: 'RelationOppositeName', type: 'string' }
    ]
}),
于 2013-03-19T16:09:42.603 回答