2012-12-05 33 views
0

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

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

我的模型:

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

调用data.someFunction当newDetails功能,发生错误()。

我有错误的期望吗?

回答

1

功能上存在的模式,所以你会打电话:

records[0].someFunction(); 
+0

非常感谢您的快速回复,这是完全正确的。 :)另一个细节是,在某些功能中,您需要调用this.data来获取实际填充的数据字段。再次感谢! – romacafe

+0

你应该真的使用this.get()而不是访问原始数据属性。 –