Wednesday, December 7, 2011

Object # Object has no method 'read'

Object #<Object> has no method 'read'


This error occurs usually when ExtJS cannot find a Model class that you are trying to use in a different Model or in a store.

To avoid this error, make sure:
(a) you use fully-qualified Model names wherever you refer to them. E.g., "contacts.model.AddressModel" instead of just "AddressModel".
(b) you have placed the file having the code for the Model class in the app\model folder
(c) you have a "requires" config set up on the respective Model or store object.

Here's an example: ContactModel references AddressModel


 Ext.define(
'contacts.model.ContactModel', {
extend: 'Ext.data.Model',
requires : [
'contacts.model.AddressModel'
],
fields: [
{ name: 'id', type: 'integer' },
{ name: 'name', type: 'string' },
{ name: 'company', type: 'string' },
{ name: 'age', type: 'integer' },
{ name: 'addresses', type: 'array' },
],
hasMany: {model: 'contacts.model.AddressModel', 
                                               name: 'addresses', associationKey:'addresses'}
});

Here's another example: ContactStore uses ContactModel


Ext.define('contacts.store.ContactStore', {
    extend: 'Ext.data.Store',

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            autoLoad: true,
            storeId: 'ContactStore',
requires: ['contacts.model.ContactModel'],
model: 'contacts.model.ContactModel',
singleton : true,
            proxy: {
                type: 'ajax',
                url: 'http://localhost/contacts.json',
                reader: {
                    type: 'json',
                    root: 'data'
                }
            }
        }, cfg)]);
    }
});



No comments:

Post a Comment