2011-10-24 40 views
1

我有两个模型:“身份”和“配置文件”。身份“belongsTo”个人资料。当我得到'身份'类型的记录时,我想(通过该记录)获得记者档案。我使用下面的代码尝试:属于ExtJS模型

Ext.define('App.model.Identity', { 
    extend: 'Ext.data.Model', 
    fields: [ 
      // ... 
      {name: 'id_profile', type: 'int'}, 
      // ... 
      ], 

    belongsTo: { 
     model: 'App.model.Profile', 
     primaryKey: 'id', 
     foreignKey: 'id_profile', 
     associatedName: 'Profile' 
    }, 

    proxy: { 
     type: 'ajax', 
     api: { 
      read: App.Config.getBaseUrl() + '/admin_identity/list', 
      create: App.Config.getBaseUrl() + '/admin_identity/create', 
      update: App.Config.getBaseUrl() + '/admin_identity/update', 
      destroy: App.Config.getBaseUrl() + '/admin_identity/destroy' 
     }, 
     reader: { 
      type: 'json', 
      root: 'data' 
     } 
    } 
}); 

Ext.define('App.model.Profile', { 
    extend: 'Ext.data.Model', 
    fields: [ 
      {name: 'id', type: 'int'}, 
      // ... 
      ], 

    belongsTo: { 
     model: 'App.model.Identity', 
     primaryKey: 'id', 
     foreignKey: 'id_profile', 
     name: 'identities' 
    }, 

    proxy: { 
     // ... 
    } 
}); 

当我尝试这样做:

function viewProfile(identity) { 
    identity.getProfile(function(profile){ 
     console.log(profile); 
    }); 
} 

我得到的是一个空的配置文件的对象。奇怪的是,Identity类没有做任何http请求来获取配置文件。我这样做对吗?

在此先感谢

回答

1

你试过:

identity.getProfile({ 
    success:function(profile, operation){ 
    }, 
    failure: function(profile, operation){ 
     //check for a failure 
    } 
}); 

我也想尝试删除associatedName财产。

+0

associatedName属性用于生成getter和setter。这样我可以调用'modelObject.getProfile'(最初,我需要调用get {modelname})。您提出的解决方案不起作用。回调不被调用。 –

+0

我可以看到你的代理配置吗?这里是 –

+0

。我只为Identity模型添加了问题,Profile模型具有几乎相同的代理。 App .. baseUrl是一个已定义的函数,仅用于帮助我处理URL。 –