How to detect model parameter change event in mithril.js?

ぃ、小莉子 提交于 2020-01-21 14:39:12

问题


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"})

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)?

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.

am I missing something or am I understanding how to use mithril.js wrongly?

thanks in advance.


回答1:


One of the key ideas with Mithril is that changes usually happens after an event:

  • A user action like onclick or keyup defined in a m() view template
  • An ajax request made with m.request

Mithril automatically redraws after those, alleviating the need for most listeners.

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.



来源:https://stackoverflow.com/questions/30704075/how-to-detect-model-parameter-change-event-in-mithril-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!