2012-10-11 58 views
0

我使用ExtJS 4.0.7 ExtJS MVC功能。我有一個父母模型hasManybelongsTo父母。ExtJS MVC belongsTo/hasMany訪問兒童

訪問孩子的正確方法是什麼?如果我通過parent.children().data.items[0].data(而不是parent.data.children[0]),有不需要的財產MyApp.model.parent_id。我也注意到日期之間存在差異。

首先,這裏是父母的定義。所以父母會有很多孩子。

Ext.define('MyApp.model.Parent', { 
    extend : 'Ext.data.Model', 
    fields : [ 'id' ], 
    hasMany : [ { 
     model : 'MyApp.model.Child', 
     name : 'children' 
    }], 
    proxy : { 
     type : 'direct', 
     api : { 
      create : Parents.create, 
      read : Parents.read, 
      update : Parents.update, 
      destroy : Parents.destroy 
     } 
    } 
}); 

而且每個孩子都屬於父母。

Ext.define('MyApp.model.Child', { 
    extend : 'Ext.data.Model', 
    fields : [ 'id', { 
     name : 'validFrom', 
     type : 'date', 
     dateFormat : 'time' 
     } ], 
    belongsTo : 'Parent' 
}); 

我在我的控制器加載父:

this.getParentModel().load(id, { 
    scope : this, 
    success : function(parent) { 
        // the party looks good here when logged to console 
        console.log(parent); 
     this.updateStore(parent); 
    } 
}); 

當檢查控制檯的父母,這是我發現:

  1. console.log(parent.data.children[0])

控制檯輸出:

Object 
id: 3 
validFrom: -1767225600000 
__proto__: Object 
  • console.log(parent.children().data.items[0].data)
  • 控制檯輸出:

    Object 
    id: 3 
    MyApp.model.parent_id: 209 // why is that here? 
    validFrom: Thu Jan 01 1914 01:00:00 GMT+0100 (Central European Standard Time) 
    __proto__: Object 
    

    enter image description here

    +0

    雖然我確定你的問題很清楚,但很難理解/回答顯示的那個小代碼。你能否分享這兩種模式的定義? – Izhaki

    +0

    @Izhaki對於缺乏信息,我感到抱歉。我已經編輯了我的問題,並希望更清楚(我也爲我清理了一些東西)。 – ipavlic

    回答

    0

    如果我理解正確的話,我想你想:

    var someRecordId = 'idProperty'; 
    var record = Ext.getStore('Parent').getById(someRecordId); 
    
    // this returns all hasMany for the name of of the property i.e. name: 'child' 
    
    record.child(); 
    

    這將返回父項記錄的子項中的所有項目數據(基於模型定義)。它應該只包含子模型定義中的那些字段。所以parent_id將不會被包含。如果您試圖檢索Child的所有記錄,那麼您只需以與其他商店相同的方式獲取它們即可。