我是 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"
其他人如何处理这个问题?