在Mithril中使用m.component()时如何遍历VDOM?

时间:2015-07-22 21:21:46

标签: javascript mithril.js

我是Mithril的新手,但对它强制实施良好编码模式和关注点分离的方式感到非常高兴。参考这个我开始编码,大量使用m.component()。

后来我读了Mithril文章“当CSS让你失望”(http://lhorie.github.io/mithril-blog/when-css-lets-you-down.html)时,它解释了如何将Transformer-Functions写入travers并操纵虚拟DOM-Tree。写出几种跨领域问题的精彩概念。

但是当我尝试将此模式与包含VDOM的组件一起使用时,它不起作用,因为m.component返回的是组件而不是VDOM-Object。检测组件没有帮助,因为此时不构建嵌入视图。

现在我问自己,如何处理这个问题,或者我是否理解了一些根本错误的东西......

这里有几行代码显示问题:

...

someComponent.view = function() {
return m('html', [
    m('body', [
        m('div', [
            m.component(anotherComponent)
        ])
    ])
};

...

// and now the traversal function from the mithril side
var highlightNegatives = function (root, parent) {
    if (!root) return root;
    else if (root instanceof Array) {
        for (var i = 0; i < root.length; i++) {
            highlightNegatives(root[i], parent);
        }
    } else if (root.children) {
        highlightNegatives(root.children, root);
    } else if (typeof child == "string" && child.indexOf("($") === 0) {
        parent.attrs.class = "text-danger";
    }
    return root;
};

highlightNegatives(someComponent.view()); // will not find the relevant elements in "anotherComponent"

其他人如何处理这个问题?

1 个答案:

答案 0 :(得分:0)

撰写博客文章后添加了组件。

您需要先将组件转换为vDOM对象,然后才能将其包含在视图中。注意我已经完成了两次,一次是在highlightNegatives()函数内部,当我们遍历vDOM树时捕获组件,当我们在视图中调用它时也在函数参数中。

除非要向组件发送属性或其他选项,否则您可以在不调用m.component()的情况下包含组件标识符:

myComponent
vs.
m.component( myComponent, {extraStuff: [1,2,3]}, otherStuff )

调用highlightNegatives()时需要考虑这个问题。

您的模板中无需包含 html 正文。浏览器会为您执行此操作(或者您可以将它们包含在index.html中),

以下是以下代码的工作示例: http://jsbin.com/poqemi/3/edit?js,output

var data = ['$10.00', '($20.00)', '$30.00', '($40.00)', '($50.00)']

var App = {
  view: function(ctrl, attrs) {
    return m("div.app", [
      m('h1', "My App"),
      someComponent
    ])
  }
}

var anotherComponent = {
  controller: function (){
    this.data = data
  },
  view: function (ctrl){
    return m('div.anotherComponent', [
      m('h3', 'anotherComponent'),
      ctrl.data.map( function(d) {
        return m('li', d)
      })
    ])
  }
}

var someComponent = {}
someComponent.controller = function (){ }
someComponent.view = function (ctrl) {
  return m('div.someComponent', [
      m('h2', 'someComponent'),
          highlightNegatives(anotherComponent.view( new anotherComponent.controller() ))
        ])
};

// and now the traversal function from the mithril side
var highlightNegatives = function (root, parent) {
    if (!root) return root;
    else if (root instanceof Array) {
      for (var i = 0; i < root.length; i++) {       
        // when you encounter a component, convert to VDOM object
        if (root[i].view) {
          highlightNegatives(root[i].view( new root[i].controller() ))
        }else{
          highlightNegatives(root[i], parent);
        }
      }
    } else if (root.children) {
        highlightNegatives(root.children, root);
    } else if (typeof root == "string" && root.indexOf("($") === 0) {
          parent.attrs.class = "text-danger";
    }
    return root;
};

m.mount(document.body, App)

//Calling this function after the App is mounted does nothing.
//It returns a vDOM object that must be injected into the someComponent.view
//highlightNegatives(someComponent.view()); // will not find the relevant elements in "anotherComponent"