How do I create an instance of a model and load it?

I’m relatively new to ExtJS and I’m having trouble with what I believe is a core concept. In my application I’d like to define a model, create an instance and load it. I’ve looked high and low for an example that works on the web, but am stumped.

Everytime I try to load the model, the load function doesn’t look to exist. The error I get in the console is “TypeError: timezoneDetails.load is not a function”.

Here is the code I an using to try and illustrate this:

Ext.define('App.model.Model', {
    extend: 'Ext.data.Model',

    idProperty: 'ID',

    proxy: {
        type: 'rest',
        url: '/timezones'
    },

    fields: [{
        name: 'BaseUtcOffset',
        type: 'string'
    }, {
        name: 'DisplayName',
        type: 'string'
    }, {
        name: 'DisplayName',
        type: 'string'
    }, {
        name: 'ID',
        type: 'string'
    }, {
        name: 'SupportsDaylightSavingTime',
        type: 'boolean'
    }],
});

Ext.application({
    name: 'App',

    appFolder: 'app',

    launch: function() {
        var timezoneDetails = Ext.create('App.model.Model', {
            ID: 'Bob'
        });
        timezoneDetails.load(); // TypeError: timezoneDetails.load is not a function
    }
});

A Sencha Fiddle of this code is available here: https://fiddle.sencha.com/#fiddle/1lq

Answer

load is static on each model class. You’ll get the record in the callback.

App.model.Model.load(1, function(rec) {
    console.log(rec.getId());
});

Leave a Reply

Your email address will not be published. Required fields are marked *