简体   繁体   English

我们如何处理包含带有秘银子阵列的数据模型?

[英]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: 我想创建一个简单的树:

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.

相关问题 Mithril-子组件修改父组件数据后,对子组件数组进行重新排序 - Mithril - Re-sort array of child components after child component modifies parent component data 如何处理对象数组? 我有一个包含3个对象的数组,这些对象包含来自数据库的JSON数据 - How to deal with array of objects? I have an array with 3 objects containing JSON data from my db 如何在 mithril js 模型和控制器中获取 m.request 获取数据“长度”? - How to get m.request get data 'length' in mithril js model and controller? 如何在秘银中使用m.request对模型进行单元测试? - How can I unit test a model using m.request in Mithril? 如何在秘银中对具有视图模型依赖性的视图进行单元测试? - How can I unit test a view with a view-model dependency in Mithril? 如何在mithril.js中检测模型参数更改事件? - How to detect model parameter change event in mithril.js? Loopback:我们如何修改内置模型函数的返回数据 - Loopback: How can we modify the return data of a built in model function 我们如何处理来自错误的不同HTTP响应? - How can we deal with different HTTP response from error? 在mithril-js中,vnode如何绑定到click事件 - In mithril-js, how can vnode bind to click event 如何过滤数据中包含数组的数据? - How to filter the data containing array inside the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM