2011-10-24 41 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。 –