在 Mithril 中,你不应该尝试直接接触 DOM。您的事件处理程序应修改 View-Model 的状态,该状态应在您的 View 方法中访问。如果您发布更多代码,我可以更详细地解释它是如何组合在一起的。
这是一个简单的示例,显示了流经秘银的数据。您的情况需要更复杂,但我目前无法解析所有 peer.js 代码。
http://codepen.io/anon/pen/eNBeQL?editors=001
var demo = {};
//define the view-model
demo.vm = {
init: function() {
//a running list of todos
demo.vm.description = m.prop('');
//adds a todo to the list, and clears the description field for user convenience
demo.vm.set = function(description) {
if (description) {
demo.vm.description(description);
}
};
}
};
//simple controller
demo.controller = function() {
demo.vm.init()
};
//here's the view
demo.view = function() {
return m("html", [
m("body", [
m("button", {onclick: demo.vm.set.bind(demo.vm, "This is set from the handler")}, "Set the description"),
m("div", demo.vm.description())
])
]);
};
//initialize the application
m.module(document, demo);
请注意,该按钮正在调用 View-Model ( set
) 上的方法,该方法正在设置属性 ( vm.description
) 的值。这会导致 View 重新渲染,并且 div 显示新值 ( m("div", demo.vm.description())
)。