【发布时间】:2018-08-11 13:25:09
【问题描述】:
我正在开发 Ember 2.13.0
控制器的 Ember.js 文档显示“The controller receives a single property from the Route – model – which is the return value of the Route's model() method.”。
我不清楚如何访问这些数据。
我的路线如下所示:
export default Ember.Route.extend({
model: function() {
return this.store.findAll('costcentrehierarchyfortree');
}
});
还有一个看起来像这样的控制器:
export default Ember.Controller.extend({
data: function() {
var arrJSTreeElements = this.model.map(obj =>{
/*
* This is the type of structure that needs to be output
*
[
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
*/
var rObj = {};
rObj['id'] = obj.cchcceidchild;
if (obj.cchcceidparent === obj.cchcceidchild)
{
//This is the top of the tree
rObj['parent'] = "#";
}
else
{
rObj['parent'] = obj.cchcceidparent;
}
rObj['text'] = obj.ccidescriptionchild;
return rObj;
});
return arrJSTreeElements ;
}
});
但是当我访问有问题的路线时,我得到一个控制台错误
Cannot read property 'map' of undefined
所以我猜想使用this.model 访问从路由的 model() 方法返回的数据不是要走的路……那我做错了什么?
提前致谢。
编辑: 只是为了让我在这里尝试做的更清楚一点,您可以在我正在使用的插件的演示应用中看到一个(有点)类似的示例。这是控制器https://github.com/ritesh83/ember-cli-jstree/blob/master/tests/dummy/app/dynamic/controller.js,这是模板https://github.com/ritesh83/ember-cli-jstree/blob/master/tests/dummy/app/dynamic/template.hbs。
不幸的是,这不是一个直接的例子,因为它依赖于静态文件而不是由于调用路由而返回的数据,但它至少给出了一些想法。
编辑 2:
在 Lux 和我找到的其他资源的帮助下,我得以完成这项工作。
如果这对任何人都有帮助,这是我最终使用的版本并且有效。请注意,处理不再在控制器上,而是在路由上
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('costcentrehierarchyfortree');
},
setupController(controller, model) {
this._super(controller, model);
//
this.store.findAll('costcentrehierarchyfortree').then(function (arrRawCch) {
var idx = 0;
var arrJSTreeElements = arrRawCch.map(obj =>{
idx++;
var rObj = {};
rObj['id'] = obj.get('cchCceIdChild');
if (obj.get('cchCceIdParent') === obj.get('cchCceIdChild'))
{
//This is the top of the tree
rObj['parent'] = "#";
}
else
{
rObj['parent'] = obj.get('cchCceIdParent');
}
rObj['text'] = obj.get('cceIdentifierChild');
//
// Add non-jstree-standard data into 'data' object
// see this forum post for more details
// https://groups.google.com/forum/#!topic/jstree/qBM2ZCAkPL4
//
rObj['data'] = {};
rObj['data']['cchDrvId'] = obj.get('cchDrvId');
//
return rObj;
});
Ember.set(controller, 'data', arrJSTreeElements)
});
},
});
【问题讨论】:
标签: ember.js