romacafe
romacafe

Reputation: 3128

Extjs4 - Defining methods on a model

According to the docs, I should be able to define functions on a Model, and then call the function on instances of that model.

At least in the case where I load the model from a proxy, its not working.

My model:

Ext.define('MyApp.model.Detail', {
    extend: 'Ext.data.Model',
    fields: [
        'someField',
        'anotherField',
    ],

    someFunction: function() {
            //do something
    },

    proxy: {
        type: 'ajax',
        url: 'http://example.com/details',
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: 'total',
            successProperty: 'success',
        }
    },
});

Store:

Ext.define('MyApp.store.DetailStore', {
    extend: 'Ext.data.Store',
    storeId: 'detailStore',
    model: 'MyApp.model.Detail',
});

Controller:

Ext.define('MyApp.controller.appController', {
    extend: 'Ext.app.Controller',
    init: function() {
        this.getDetailStoreStore().addListener('load', this.newDetails, this);
    },

    stores: ['DetailStore'],

    models: ['Detail'],

    views : ['DetailView], //exists, not copied into question

    newDetails: function(store, records) {
        var details = records[0].data;
        details.someFunction();  // Uncaught TypeError: Object #<Object> has no method 'someFunction'
    }
});

The error happens in the newDetails function, when calling data.someFunction().

Do I have the wrong expectation?

Upvotes: 0

Views: 79

Answers (1)

Evan Trimboli
Evan Trimboli

Reputation: 30082

The function exists on the model, so you would call:

records[0].someFunction();

Upvotes: 1

Related Questions