问题描述
我最近开始学习 mithril.js,我想知道如何制作非常基本的模型 -> 查看单向数据绑定应用程序.
I recently started learning mithril.js and I'm wondering how can I make very basic Model -> View one way data binding app.
TestModel = function(data){
this.name = m.prop(data.name)
}
testModel = new TestModel({name: "John"})
上面的代码声明了一个模型,它可以完美地作为getter/setter.但是如何为模型事件设置事件侦听器,例如 Backbone 的 listenTo('model',"change",callbackFunc)
?
code above declare a model and it works perfectly as getter/setter.but how can I set an event listener for the model event like Backbone's listenTo('model',"change",callbackFunc)
?
我看到的所有示例代码都是为click
、keyup
或onchange
等实际用户操作设置事件.但从不监听实际模型值直接声明.
all sample codes I saw are setting events for actual user actions like click
,keyup
or onchange
.but never listen to actual model value's state directly.
我是否遗漏了什么,或者我是否理解错误地使用 mithril.js?
am I missing something or am I understanding how to use mithril.js wrongly?
提前致谢.
推荐答案
Mithril 的一个关键思想是更改通常发生在事件之后:
One of the key ideas with Mithril is that changes usually happens after an event:
m()
视图模板中定义的类似onclick
或keyup
的用户操作- 使用
m.request
发出的 ajax 请求
- A user action like
onclick
orkeyup
defined in am()
view template - An ajax request made with
m.request
秘银在这些之后自动重绘,减轻了大多数听众的需求.
Mithril automatically redraws after those, alleviating the need for most listeners.
如果您通过其他方法更新模型并且需要手动重绘,请使用 m.redraw
或 m.startComputation/m.endComputation
.感谢 Mithril 的 DOM diff 算法,重绘非常便宜,所以不要害怕使用它们(当然有一些常识!)查看 m.redraw 文档了解更多信息.
If you are updating your models through some other method and you need to redraw manually, use m.redraw
or m.startComputation / m.endComputation
. Thanks to Mithril's DOM diff algorithm, redraws are very cheap so don't be afraid to use them (with some common sense, of course!) Check out the m.redraw documentation for more info.
这篇关于如何检测 mithril.js 中的模型参数更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!