javascript - 为什么 m.module 不会导致我的视​​图在 Mithril 中重新渲染?

标签 javascript mithril.js

我认为给定一个具有 controllerview 属性的对象,当 Controller 的状态发生变化时(或者 m.prop 属性发生变化) )它将重新渲染模板。这在我的例子中没有发生(我必须直接使用redraw)。我在这里做错了什么? ////////app.js

define([ 'mithril', 'propertyPane'],
  function(m, propertyPane){
    //div#properties exists
    m.module(document.getElementById('properties'),propertyPane);
});

///////propertyPane.js

define([ 'mithril', 'emitter'],
  function(m, emitter) {

    //mithril "namespace"
    var propertyPane = {};

    ///////////////////////
    //    Models         //
    ///////////////////////
    propertyPane.PropertyList = Array;

    propertyPane.Property = function(name, data) {
      //the following reflects the format of the data obects as sent by the event emitter
      this.name        = m.prop(name);
      this.value       = m.prop(data.value);
      this.type        = m.prop(data.valueType);
      this.visible     = m.prop(data.userVisible);
      this.editable    = m.prop(data.userEditable);
    };

    ///////////////////////
    //    Controller     //
    ///////////////////////
    propertyPane.controller = function() {

      this.properties = propertyPane.PropertyList();

      this.updatePropertyPane = function(sender) {
        //destroy current list
        this.properties.length = 0;

        for( var key in sender.currentItem.tag){
          this.properties.push(new propertyPane.Property(key, sender.currentItem.tag[key]));
        }
//>>>This is required to make the property pane to rerender:<<<
//      m.redraw(); //why?
//why doesn't redraw happen automatically (because I'm using m.module)??
      }.bind(this);

      //this reliably updates the the properties list
      //but rerender does not happen
      emitter.addCurrentItemChangedListener(this.updatePropertyPane);

    };

    ///////////////////////
    //    View           //
    ///////////////////////
    propertyPane.view = function(ctrl){
      return m('ul',
        ctrl.properties.map(function(property){
          return m('li', property.name() + ' ' + property.value());
        })
      );
    };

    return propertyPane;
});

我还尝试添加一个名为 foo 的属性,该属性在 updatePropertyPane 中更新:

//in controller
this.foo = m.prop(Date.now());
//in updatePropertyPane
  this.foo(Date.now());
//in view
m('li', ...+this.foo()) //to make sure the view accesses this property.

这也不起作用。

最佳答案

令人惊讶的是,m.prop返回一个 getter/setter,没有副作用。

Mithril hooks本身在您 View 的事件监听器中。

因此,根据我对 Mithril 的非常有限的经验,您应该使用 m 来设置事件监听器或使用 m.startComputation / m.endComputation其调用 m.redraw 的额外好处是允许应用程序的不同部分only redraw once尽管更新了一些东西。

关于javascript - 为什么 m.module 不会导致我的视​​图在 Mithril 中重新渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252226/

相关文章:

javascript - 返回弹出窗口时捕获“确定”按钮 [离开此页面或留在此页面]

javascript - 将每个对象中的字符串循环到一个字符串

Javascript 等待异步从 Firebase 检索数据

javascript - Mithril js - 跨组件通信模式

javascript - Jqueryui draggable 在我的元素上留下类。为什么?

javascript - 在 JavaScript 中继承时实例化函数有什么意义?

Mithril.js 多个 css 类

mithril.js - 挂载在 Controller 虚拟机内部不工作

Javascript - Mithril 嵌套组件

javascript - Mithril ajax 发送空输入