Error while using extended model in ExtJS 4

154 views Asked by At

I have created a model in ExtJS and then created another model which extends the first model. When use the second model in a store, I am getting JavaScript error.

TypeError: this.type.convert is not a function
this.defaultValue = this.type.convert(this.defaultValue);

-- Model--

Ext.define('myModel.FirstModel', {
    extend: 'Ext.data.Model',
    fields: [

                { name: 'ID' },
                { name: 'Name' },
                { name: 'IsSelected', type: 'bool', defaultValue: false },
                { name: 'IsUpdated', type: 'bool', defaultValue: false },
              ]
});

--Second model--

Ext.define('myModel.TreeModel', {
    extend: 'myModel.FirstModel',
    fields: [

                { name: 'leaf', type: 'bool', defaultValue: false },
                { name: 'expanded', type: 'bool', defaultValue: false }
    ]
});

--Store --

Ext.define('myStore.TreeStore', {
    extend: 'Ext.data.TreeStore',
    requires: ['myModel.TreeModel'],
    model: 'myModel.TreeModel',
    autoLoad: false,
    proxy: {
        type: 'memory'
    }
});

** If I do not extend from first model and just copy paste all the fields of first model into second model. I am getting the desired result. What is the cause of the error? What is the right way to extend a model **

1

There are 1 answers

0
rixo On

Your way is the way... So, upgrade your version of Ext! The 4.1.0 is the only one I found that is concerned with this issue.

Or you can work around the problem by adding this line at the beginning of your code:

Ext.data.Types.AUTO.convert = function(v) {return v;};

Or you can work around it by ensuring all your inherited fields have either a defaultValue or a convert method:

Ext.define('myModel.FirstModel', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'ID', defaultValue: null },
        { name: 'Name', defaultValue: null },
        { name: 'IsSelected', type: 'bool', defaultValue: false },
        { name: 'IsUpdated', type: 'bool', defaultValue: false },
    ]
});

Oh, and don't hesitate to give your precise version of Ext, even if you don't think it should matter with your question... One never knows!