如何检测模型参数的变化事件mithril.js? [英] How to detect model parameter change event in mithril.js?

查看:108
本文介绍了如何检测模型参数的变化事件mithril.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始学习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"})

code以上声明模型和它完美的作品为的getter / setter。
但我怎么能设置一个事件监听器像骨干 listenTo(模型,变,callbackFunc)

所有样品codeS我看到了正在为实际用户的操作事件,如点击 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?

先谢谢了。

推荐答案

一个用秘银的主要观点是,变化通常一个事件后发生的:

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


  • 的用户动作如的onclick KEYUP M规定()视图模板

  • m.request 做一个Ajax请求

  • 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.

如果您通过其他方法更新模型,您需要手动重新绘制,使用 m.redraw m.startComputation /平方米。 endComputation 。由于秘银的DOM差异算法,重绘是非常便宜的,所以不要害怕使用它们(一些常识,当然!)退房的 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆