Extjs 5, data model association & load nested data

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

trying to make this work....

I want to load nested data on two object model

Ext.application({ name : 'MyApp',  launch : function() {       Ext.define('MyApp.model.Address', {         extend: 'Ext.data.Model',          entityName: 'Address',          fields: [               {                 name: 'id',                 type: 'int'             },             {                 name: 'addressLine',                 type: 'string'             },             {                 name: 'city',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ]     });       Ext.define('MyApp.model.User', {         extend: 'Ext.data.Model',          entityName: 'User',          fields: [              {                 name: 'id',                 type: 'int'             },             {                 name: 'address',                 reference: 'Address'             },             {                 name: 'name',                 type: 'string'             },             {                 name: 'lastname',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ]     });       var user = new MyApp.model.User({         "id": 1,         "name": "Pedro",         "lastname": "Carbonell",         "address": {             "id": 1,             "addressLine": "Bailen 22",             "city": "Barcelona",             "created": 1420668866000         },         "created": 1420668866000     });              console.info(user);     console.info(user.getAddress());  }});

It's result on no error when created the user, but when I access to associated data via user.getAddress() it returned an exception:

 Uncaught Error: The model ID configured in data ("[object Object]") has been rejected by the int field converter for the id fieldext-all-debug.js

Try to define proxy like memory or localstorage on model definitions, but the result it is the same.

Ext fiddle: https://fiddle.sencha.com/#fiddle/h2d

Any help will be appreciated!

回答1:

Solved, but only find this solution: when use loadRawData...

    var store = new Ext.data.Store({         model: MyApp.model.User     });      store.loadRawData({         "id": 1,         "name": "Pedro",         "lastname": "Carbonell",         "address": {             "id": 1,             "addressLine": "Bailen 22",             "city": "Barcelona",             "created": 1420668866000         },         "created": 1420668866000     });              console.info(store.first());     console.info(store.first().getAddress());

sample at this new fiddle: https://fiddle.sencha.com/#fiddle/h4e

you'r right, ext is a bit flaky, very....



回答2:

I've been playing around with the code in your fiddle and not been able to get the association working the official way as of yet.

I simulated the functionality using this code:

Ext.define('MyApp.model.User', {         extend: 'Ext.data.Model',          fields: [{             name: 'id',             type: 'int'         }, {             name: 'name',             type: 'string'         }, {             name: 'lastname',             type: 'string'         }, {             name: 'created',             type: 'date',             dateFormat: 'time',             persist: false         }],          getAddress: function() {             if ('undefined' === this.data.address) {                 return null;             }             return Ext.create('Address', this.data.address);         }     });

Basically I've removed the association and created a custom function to create a model record based off of the raw data passed in, You could also return a new, empty model if the address data does not exist instead of null, I used null as it's easier to determine whether you have a valid address record or not.

As already mentioned - this is not the official way to do this, I will have another play around with the fiddle and post a better solution once I find it, this may help in the meantime.



回答3:

Using the original code, I made a few modifications and now it appears to be working.

Ext.application({ name : 'MyApp',  launch : function() {       Ext.define('MyApp.model.Address', {         extend: 'Ext.data.Model',          //entityName: 'Address',          fields: [               {                 name: 'id',                 type: 'int'             },             {                 name: 'addressLine',                 type: 'string'             },             {                 name: 'city',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ],          hasMany: 'User'     });       Ext.define('MyApp.model.User', {         extend: 'Ext.data.Model',          //entityName: 'User',          fields: [              {                 name: 'id',                 type: 'int'             },             {                 name: 'name',                 type: 'string'             },             {                 name: 'lastname',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ],           hasMany: { model: 'Address', name: 'Address' }     });       var user = new MyApp.model.User({         "id": 1,         "name": "Pedro",         "lastname": "Carbonell",         "address": {             "id": 1,             "addressLine": "Bailen 22",             "city": "Barcelona",             "created": 1420668866000         },         "created": 1420668866000     });              console.info(user);     console.info(user.data.address);  } });


回答4:

Is this the sort of thing you're after? I set Address manually on the User model. Not ideal but it's interpreted correctly as a record then.

    Ext.define('MyApp.model.Address', {         extend: 'Ext.data.Model',          entityName: 'Address',          fields: [               {                 name: 'id',                 type: 'int'             },             {                 name: 'addressLine',                 type: 'string'             },             {                 name: 'city',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ]     });       Ext.define('MyApp.model.User', {         extend: 'Ext.data.Model',          entityName: 'User',          fields: [              {                 name: 'id',                 type: 'int'             },             {                 name: 'addressId',                 reference: 'Address'             },             {                 name: 'name',                 type: 'string'             },             {                 name: 'lastname',                 type: 'string'             },             {                 name: 'created',                 type: 'date',                 dateFormat: 'time',                 persist: false             }         ]     });       var user = new MyApp.model.User({         "id": 1,         "name": "Pedro",         "lastname": "Carbonell",         "created": 1420668866000     });              var addr  = new MyApp.model.Address({             "id": 1,             "addressLine": "Bailen 22",             "city": "Barcelona",             "created": 1420668866000     });     user.setAddress(addr);     console.info(user);     console.info(user.getAddress());


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!