2014-05-09 114 views
1

我有兩個模型(分類 - >對象)與各協會Sencha Touch商店協會不符合?

Category 
    |--Has many--->Objects 

這裏是CategoryModel

Ext.define("TouchApp.model.CategoryModel", { 
    extend: "Ext.data.Model", 
    config: { 
     fields: [ 
      { 
       name: 'id', 
       type: 'int' 
      }, 
      { 
       name: 'name', 
       type: 'String' 
      } 
     ], 
     //Aide: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association 
     associations: [ 
      { type: 'hasMany', model: 'ObjectsModel', primaryKey: 'id', foreignKey: 'category'} 
     ] 

    } 
}); 

這裏是ObjectsModel

Ext.define("TouchApp.model.ObjectsModel", { 
    extend: "Ext.data.Model", 
    config: { 
     fields: [ 
      { 
       name: 'id', 
       type: 'int' 
      }, 
      { 
       name: 'name', 
       type: 'String' 
      }, 
      { 
       name: 'category', 
       type: 'int' 
      } 
     ], 
     //Aide: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association 
     associations: [ 
      { type: 'belongsTo', model: 'CategoryModel', primaryKey: 'id', foreignKey: 'category'} 
     ] 
    } 
}); 

這個協會對嗎?否則,也許我不需要把它放在雙方? 我有一個警告[WARN][Ext.data.Operation#process] Unable to match the updated record that came back from the server.

PS:我需要這些模型把在單一嵌套列表相關的商店。

回答

0

我終於找到了尋找at this exemple!

它是利用協會的每一個案件一個真正偉大的例子的解決方案。

我終於理解了這個概念。

我們需要在我們的模型中設置idProperty並使用hasMany和belongsTo關聯。

CategoryModel:

Ext.define("TouchApp.model.CategoryModel", { 
    extend: "Ext.data.Model", 
    config: { 
     idProperty: 'id', //The idProperty representing the "PK" 
     fields: [ 
      { 
       name: 'id', 
       type: 'int' 
      }, 
      { 
       name: 'name', 
       type: 'String' 
      } 
     ], 
     //Really great example: http://docs.sencha.com/touch/2.2.1/#!/api/Ext.data.association.Association 
     hasMany: [{ model: 'TouchApp.model.ObjectsModel' }] //The hasMany association setting the model associated... 
    } 
}); 

ObjectsModel:

Ext.define("TouchApp.model.ObjectsModel", { 
    extend: "Ext.data.Model", 
    config: { 
     idProperty: 'id', //The idProperty representing the "PK" 
     fields: [ 
      { 
       name: 'id', 
       type: 'int' 
      }, 
      { 
       name: 'name', 
       type: 'String' 
      }, 
      { 
       name: 'category', 
       type: 'int' 
      } 
     ], 
     //Realy great example: http://appointsolutions.com/2012/07/using-model-associations-in-sencha-touch-2-and-ext-js-4/ 
     belongsTo: [{ model: 'TouchApp.model.CategoryModel', associationKey: 'category' }] //Here the belongsTo association which tell the parent Model. AssociationKey set which key is the "FK". It will be linked to the idProperty 
    } 
}); 

就是這樣。但它現在不適用於我的嵌套列表...

相關問題