Ext.js 从 json 建立模型关系时遇到问题
Ext.js problems building model relations from json
在我的项目中,我需要创建一个问卷(表格)。问题是根据类别动态生成的,并且在数据库中的服务器上。
要获取问题,请使用 REST API,因此将请求发送到 /getquestions/1 returns 一个 JSON 具有生成表单所需的所有数据。
JSON 看起来像这样:
{
"questions":[
{
"id":4,
"text":"Question1",
"type":"NUMBER",
"mandatory":true,
"visible":true
},
{
"id":5,
"text":"Dropdown type question",
"type":"DROPDOWN",
"mandatory":true,
"visible":true,
"values":[
{
"id":1,
"text":"Answer1",
"selected":false
},
{
"id":2,
"text":"Answer2",
"selected":false
}
]
},
{
"id":7,
"text":"Question 3 as Radio",
"type":"RADIO",
"mandatory":false,
"visible":false,
"values":[
{
"id":1,
"text":"Yes",
"selected":false
},
{
"id":2,
"text":"No",
"selected":false
}
],
"dependencies":{
"mark_visible":[
{
"question_id":5,
"operation":"=",
"value":2
}
],
"mark_mandatory":[
{
"question_id":5,
"operation":"=",
"value":2
},
{
"question_id":4,
"operation":"<=",
"value":5000
}
]
}
}
]
}
]
}
我有商店和模型可以提问。
我想我也需要一个值存储,以使其在下拉列表中可见,另一个存储依赖项。
我只是想知道,是否有可能在没有任何显式编码的情况下使用 hasMany 等 ext 和模型关系以某种方式填充所有存储。
我主要是在 Ext.js 中寻找最佳实践和最快的解决方案,以此 JSON
创建模型和关系
任何想法都会很有帮助。
要从 JSON 数据填充模型及其关联,您需要让它通过 reader class(例如 Ext.data.JsonReader
)向下移动'tree' 创建关联。
如果您通过代理加载,这应该会自动发生(因为它默认使用 reader)。如果您的 JSON 来自 AJAX 调用并且您正在使用 Ext.create
调用创建模型实例,那么您需要自己执行 reader 位。类似于:
var data = [...];
var reader = Ext.create('Ext.data.JsonReader', {
model: 'MyModel'
});
var resultset = reader.readRecords(data);
console.log(resultset.getRecords()[0]); // logs first read record
在我的项目中,我需要创建一个问卷(表格)。问题是根据类别动态生成的,并且在数据库中的服务器上。 要获取问题,请使用 REST API,因此将请求发送到 /getquestions/1 returns 一个 JSON 具有生成表单所需的所有数据。
JSON 看起来像这样:
{
"questions":[
{
"id":4,
"text":"Question1",
"type":"NUMBER",
"mandatory":true,
"visible":true
},
{
"id":5,
"text":"Dropdown type question",
"type":"DROPDOWN",
"mandatory":true,
"visible":true,
"values":[
{
"id":1,
"text":"Answer1",
"selected":false
},
{
"id":2,
"text":"Answer2",
"selected":false
}
]
},
{
"id":7,
"text":"Question 3 as Radio",
"type":"RADIO",
"mandatory":false,
"visible":false,
"values":[
{
"id":1,
"text":"Yes",
"selected":false
},
{
"id":2,
"text":"No",
"selected":false
}
],
"dependencies":{
"mark_visible":[
{
"question_id":5,
"operation":"=",
"value":2
}
],
"mark_mandatory":[
{
"question_id":5,
"operation":"=",
"value":2
},
{
"question_id":4,
"operation":"<=",
"value":5000
}
]
}
}
]
}
] }
我有商店和模型可以提问。 我想我也需要一个值存储,以使其在下拉列表中可见,另一个存储依赖项。
我只是想知道,是否有可能在没有任何显式编码的情况下使用 hasMany 等 ext 和模型关系以某种方式填充所有存储。
我主要是在 Ext.js 中寻找最佳实践和最快的解决方案,以此 JSON
创建模型和关系任何想法都会很有帮助。
要从 JSON 数据填充模型及其关联,您需要让它通过 reader class(例如 Ext.data.JsonReader
)向下移动'tree' 创建关联。
如果您通过代理加载,这应该会自动发生(因为它默认使用 reader)。如果您的 JSON 来自 AJAX 调用并且您正在使用 Ext.create
调用创建模型实例,那么您需要自己执行 reader 位。类似于:
var data = [...];
var reader = Ext.create('Ext.data.JsonReader', {
model: 'MyModel'
});
var resultset = reader.readRecords(data);
console.log(resultset.getRecords()[0]); // logs first read record