[英]How can we deal with a data model containing child array with mithril?
Is there an easy way with mithril to deal with that kind of data? 秘银是否有一种简单的方法来处理此类数据?
[
{"type" : "folder", "name" : "Folder1", "childs" : [
{"type": "file", "name" : "Folder1_File1"},
{"type": "file", "name" : "Folder1_File2"}
]},
{"type": "file", "name" : "File1"},
{"type": "file", "name" : "File2"},
{"type": "folder", "name" : "Folder2"}
]
I'm tying to create a simple tree: 我想创建一个简单的树:
and here's how I'm trying to deal with it: 这就是我试图处理的方式:
var tree = {};
//for simplicity, we use this component to namespace the model classes
//the Todo class has two properties
tree.Item = function(data) {
this.type = m.prop(data.type);
this.name = m.prop(data.name);
this.childs = m.prop(data.childs);
};
//the TodoList class is a list of Todo's
tree.ItemsList = function() {
return m.request({method: "GET", url: "/tree/tree.json"}).then(function(data){
out = []
data.map(function(v, k){
out[k] = new tree.Item(data[k])
if (typeof(out[k].childs()) != "undefined" ){
out[k].childs().map(function(V, K) {
out[k].childs()[K] = new tree.Item(data[k].childs[K])
})
}
})
//console.log(out)
return out
})
}
//the view-model tracks a running list of trees,
//stores a type for new trees before they are created
//and takes care of the logic surrounding when adding is permitted
//and clearing the input after adding a tree to the list
tree.vm = (function() {
var vm = {}
vm.init = function() {
//a running list of trees
vm.list = new tree.ItemsList()
//a slot to store the name of a new tree before it is created
vm.type = m.prop("");
vm.name = m.prop("");
vm.childs = m.prop([]);
vm.load = function(item) {
};
}
return vm
}())
//the controller defines what part of the model is relevant for the current page
//in our case, there's only one view-model that handles everything
tree.controller = function(args) {
tree.vm.init()
}
//here's the view
tree.view = function(ctrl, args) {
return m("ul", { "class" : "tree", "id": "tree"}, [
tree.vm.list().map(function(item) {
return m('li', {class: item.type()}, [
item.name(),
function(){
//console.log(item.childs())
if ( typeof(item.childs()) != "undefined" ){
//console.log(item.childs())
item.childs().map(function(child){
console.log(child.name())
return m('ul', [ m('li', {class: child.type()}, child.name()) ])
})
}
}()
])
})
])
};
the model is well setted by the tree.ItemsList function. 通过tree.ItemsList函数可以很好地设置模型。 And all the childs are well populated with the corresponding m.prop functions
并且所有子项都填充了相应的m.prop函数
but 但
tree.view = function(ctrl, args) {
return m("ul", { "class" : "tree", "id": "tree"}, [
tree.vm.list().map(function(item) {
return m('li', {class: item.type()}, [
item.name(),
function(){
//console.log(item.childs())
if ( typeof(item.childs()) != "undefined" ){
//console.log(item.childs())
item.childs().map(function(child){
console.log(child.name())
return m('ul', [ m('li', {class: child.type()}, child.name()) ])
})
}
}()
])
})
])
};
the block 街区
if ( typeof(item.childs()) != "undefined" ){
//console.log(item.childs())
item.childs().map(function(child){
console.log(child.name())
return m('ul', [ m('li', {class: child.type()}, child.name()) ])
})
}
Retrieve the child.name()
value in the getter/setter but: 在getter / setter中检索
child.name()
值,但:
return m('ul', [ m('li', {class: child.type()}, child.name()) ])
Is not rendered. 不呈现。
Can someone explain to me what's wrong? 有人可以告诉我怎么了吗?
The issue here is that the anonymous function that's supposed to generate the child nodes in the view
is not returning anything after executing the map
function. 这里的问题是,应该在
view
生成子节点的匿名函数在执行map
函数后没有返回任何内容。
Just adding a return
makes it work: 只需添加
return
使其工作:
function(){
if ( typeof(item.childs()) != "undefined" ){
return item.childs().map(function(child){
return m('ul', [ m('li', {class: child.type()}, child.name()) ])
})
}
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.