mithril.js - Mithril 只重绘1个模块

标签 mithril.js

如果我的页面上有 10 个 m.module,我可以调用 m.startComputationm.endComputation m.redrawm.request 仅用于其中一个模块?

看起来这些中的任何一个都会重绘我的所有模块。

我知道只有 module 1 会受到某些代码的影响,我只想让 mithril 重绘它。

最佳答案

目前,还没有对 Multi-Tenancy 的简单支持(即独立运行多个模块)。

解决方法包括使用子树指令来防止在其他模块上重绘,例如

//helpers
var target
function tenant(id, module) {
  return {
    controller: module.controller,
    view: function(ctrl) {
      return target == id ? module.view(ctrl) : {subtree: "retain"}
    }
  }
}
function local(id, callback) {
  return function(e) {
    target = id
    callback.call(this, e)
  }
}

//a module
var MyModule = {
  controller: function() {
    this.doStuff = function() {alert(1)}
  },
  view: function() {
    return m("button[type=button]", {
      onclick: local("MyModule", ctrl.doStuff)
    }, "redraw only MyModule")
  }
}

//init
m.module(element, tenant("MyModule", MyModule))

你也可以使用类似 this 的东西或 this使用 local

自动装饰事件处理程序

关于mithril.js - Mithril 只重绘1个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613387/

相关文章:

javascript - Mithril.js 中大量元素的性能问题

javascript - 在 Mithriljs 中将可编辑的动态对象添加到数组中

javascript - Mithril js,m.request()循环和concat数组有什么好的方法吗?

javascript - 无法获取 Mithril 中的响应内容

javascript - 如何在mithril js模型和 Controller 中获取m.request获取数据 'length'?

mithril.js - Mithril 需要导入 'mithril'

Mithril.js:两个子组件是否应该通过父组件的 Controller 相互通信?

javascript - 强制组件从 Mithril.js 中的不同组件重绘

jquery - Mithril 和 jQuery 如何相互交互?