2017-09-14 43 views
0

我有一个应用程序,我想控制何时重绘视图。Mithril:不能m.redraw与m.render

我可以使它工作使用m.mountm.redraw

var count = 0; 
 

 
var Counter = { 
 
    view: function() { 
 
     return m('main', [ 
 
      m('h1', ('Count: ' + count)) 
 
     ]) 
 
    } 
 
} 
 

 
m.mount(document.body, Counter); 
 

 
window.setInterval(function() { 
 
    count++; 
 
    m.redraw(); 
 
}, 200);
<html> 
 
<body> 
 
    <script src="https://unpkg.com/mithril/mithril.js"></script> 
 
    <script src="index.js"></script> 
 
</body> 
 
</html>

但是,如果使用m.render(因为我并不需要秘银来自动重),它不再有效:

var count = 0; 
 

 
var Counter = { 
 
    view: function() { 
 
     return m('main', [ 
 
      m('h1', ('Count: ' + count)) 
 
     ]) 
 
    } 
 
} 
 

 
m.render(document.body, m(Counter)); // <-- The only changed line 
 

 
window.setInterval(function() { 
 
    count++; 
 
    m.redraw(); 
 
}, 200);
<html> 
 
<body> 
 
    <script src="https://unpkg.com/mithril/mithril.js"></script> 
 
    <script src="index.js"></script> 
 
</body> 
 
</html>

如何在使用m.render而不是m.mount时使得mithril重绘?

回答

1

如前所述在秘银文档here

注意m.redraw只能如果使用m.mountm.route。如果通过m.render呈现,则应使用m.render重绘。

var count = 0; 
 

 
var Counter = { 
 
    view: function() { 
 
     return m('main', [ 
 
      m('h1', ('Count: ' + count)) 
 
     ]) 
 
    } 
 
} 
 

 
m.render(document.body, m(Counter)); 
 

 
window.setInterval(function() { 
 
    count++; 
 
    m.render(document.body, m(Counter)); // <-- Use m.render here, not m.redraw 
 
}, 200);
<html> 
 
<body> 
 
    <script src="https://unpkg.com/mithril/mithril.js"></script> 
 
    <script src="index.js"></script> 
 
</body> 
 
</html>