【问题标题】:Extjs4 - Defining methods on a modelExtjs4 - 在模型上定义方法
【发布时间】:2012-12-05 20:48:45
【问题描述】:

根据docs,我应该能够在模型上定义函数,然后在该模型的实例上调用函数。

至少在我从代理加载模型的情况下,它不起作用。

我的模特:

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',
        }
    },
});

商店:

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

控制器:

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'
    }
});

错误发生在 newDetails 函数中,调用 data.someFunction() 时。

我有错误的期望吗?

【问题讨论】:

    标签: extjs4


    【解决方案1】:

    该函数存在于模型中,因此您可以调用:

    records[0].someFunction();
    

    【讨论】:

    • 非常感谢您的快速回复,这完全正确。 :) 另一个细节是,在 someFunction 中,您需要调用 this.data 来获取实际填充的数据字段。再次感谢!
    • 你应该真正使用 this.get() 而不是访问原始数据属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多