1

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

后来我阅读了 Mithril 文章“当 CSS 让你失望”(http://lhorie.github.io/mithril-blog/when-css-lets-you-down.html),它解释了如何编写 Transformer-Functions遍历和操作虚拟 DOM 树。编写几种横切关注点的绝妙概念。

但是当我尝试将此模式与包含组件的 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"

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

4

1 回答 1

0

组件是在写完博文后添加的。

您需要先将组件转换为 vDOM 对象,然后才能将其包含在视图中。请注意,我已经这样做了 2 次,一次是在 highlightNegatives() 函数中,用于在我们遍历 vDOM 树时捕获组件,以及在我们在视图中调用它时在函数参数中。

您可以只包含组件标识符而不调用 m.component(),除非您想向组件发送属性或其他选项:

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

调用 highlightNegatives() 时需要考虑这一点。

无需在模板中包含htmlbody 。浏览器会为您执行此操作(当然,您也可以将它们包含在 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"
于 2015-08-02T17:15:43.143 回答