Man*_*anu 5 javascript extjs extjs5
试图使这项工作....
我想在两个对象模型上加载嵌套数据
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());
}});
Run Code Online (Sandbox Code Playgroud)
这是创建用户时没有错误的结果,但是当我通过 user.getAddress() 访问关联数据时,它返回了一个异常:
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
Run Code Online (Sandbox Code Playgroud)
尝试在模型定义上定义像 memory 或 localstorage 这样的代理,但结果是一样的。
分机小提琴:https : //fiddle.sencha.com/#fiddle/h2d
任何帮助将不胜感激!
解决了,但只找到这个解决方案: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());
Run Code Online (Sandbox Code Playgroud)
这个新小提琴的样本:https ://fiddle.sencha.com/#fiddle/h4e
你是对的,ext有点不稳定,非常......