quophyie quophyie - 1 month ago 7
Javascript Question

Cannot read property of undefined when passing Static property values to class definition in ExtJS

I was was wondering whether someone could give some help with a peculiar problem that I am having in ExtJS 4. I have a defined a store that takes some of the values that I have specified in the 'read' and 'create' properties of the api object in the proxy from a class where these values are defined in the statics section of the class. However, when I run the app, I keep getting the error:

**Uncaught TypeError: Cannot read property 'Url' of undefined**.


Here is the store

Ext.define('MyApp.store.address.AddressStore',
{
extend: 'Ext.data.Store',
model: 'MyApp.model.address.AddressModel',
requires: ['MyApp.props.Url'],
proxy: {
type: 'ajax',
api: {
create: MyApp.props.Url.Address.ADD_ADDRESS_URL, //This is defined in the static class below
read: MyApp.props.Url.Address.GET_ALL_ADDRESSES_URL //As is this

},
reader: {
type: 'json',
root: 'Addresses'
}
}
}

);


Heres is the the class that defines the static properties MyApp.props.Url

Ext.define('MyApp.props.Url', {
statics: {
Address: {
ADD_ADDRESS_URL: 'Address/AddAddress',
GET_ALL_ADDRESSES_URL: 'Address/GetAllAddresses',
GET_ALL_ADDRESS_TYPES_URL: 'Address/GetAllAddressTypes'
}
}


});

Answer

Think about the evaluation order of the code. In this case, you're essentially saying:

Ext.define('MyClassName', o);

The class definition only gets passed to define once the whole object is resolved. This means that the requires are only processed once we get inside the define call.

You need to do something like:

Ext.require('MyApp.props.Url', function() {
    console.log(MyApp.props.Url.Address.ADD_ADDRESS_URL);
    Ext.define('MyClass', {});
});
Comments