#378 fix strategy none semantics
This commit is contained in:
parent
cbdbdd7c60
commit
09b25cb803
2 changed files with 19 additions and 4 deletions
|
|
@ -122,7 +122,7 @@ Note that the redraw strategy is a global setting that affects the entire templa
|
||||||
|
|
||||||
### Preventing redraws on events
|
### Preventing redraws on events
|
||||||
|
|
||||||
Sometimes you only care about a particular condition in an event and want to ignore it if this condition is not met.
|
Sometimes you only care about a particular condition in an event and want the event to not trigger a redraw if this condition is not met.
|
||||||
For example, you might only be interested in running a redraw if a user presses the space bar, and you might not want to waste a redraw if the user presses any other key. In that case, it's possible to skip redrawing altogether by calling `m.redraw.strategy("none")`
|
For example, you might only be interested in running a redraw if a user presses the space bar, and you might not want to waste a redraw if the user presses any other key. In that case, it's possible to skip redrawing altogether by calling `m.redraw.strategy("none")`
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
@ -132,6 +132,14 @@ m("input", {onkeydown: function(e) {
|
||||||
}})
|
}})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There are some important semantic caveats for `m.redraw.strategy("none")` that you should be aware of: Setting the strategy to `"none"` only affects **synchronous** redraws. As soon as the event handler returns, the strategy is set back to "diff".
|
||||||
|
|
||||||
|
If you set strategy to `"none"` but then proceed to trigger a redraw asynchronously, either via `start/endComputation`, `m.redraw` or `m.request`, a redraw *will* occur, using the `"diff"` strategy.
|
||||||
|
|
||||||
|
Additionally, calling `m.redraw` synchronously after calling `m.redraw.strategy("none")` resets the strategy to `"diff"`.
|
||||||
|
|
||||||
|
Lastly, be aware that if a user action triggers more than one event handler (for example, oninput and onkeypress, or an event bubbling up to event handlers in multiple ancestor elements), every event triggers a redraw by default. Setting strategy to none in any one of those handlers will not affect the redrawing strategy of other handlers (and remember that `strategy("none")` has no effect on asynchronous redraws).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Forcing redraw
|
### Forcing redraw
|
||||||
|
|
|
||||||
13
mithril.js
13
mithril.js
|
|
@ -409,7 +409,7 @@ var m = (function app(window, undefined) {
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
try {return callback.call(object, e)}
|
try {return callback.call(object, e)}
|
||||||
finally {
|
finally {
|
||||||
m.endComputation()
|
endFirstComputation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -499,7 +499,7 @@ var m = (function app(window, undefined) {
|
||||||
controllers[index] = controller;
|
controllers[index] = controller;
|
||||||
modules[index] = module
|
modules[index] = module
|
||||||
}
|
}
|
||||||
m.endComputation();
|
endFirstComputation();
|
||||||
return controllers[index]
|
return controllers[index]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -523,7 +523,7 @@ var m = (function app(window, undefined) {
|
||||||
function redraw() {
|
function redraw() {
|
||||||
var mode = m.redraw.strategy();
|
var mode = m.redraw.strategy();
|
||||||
for (var i = 0, root; root = roots[i]; i++) {
|
for (var i = 0, root; root = roots[i]; i++) {
|
||||||
if (controllers[i] && mode != "none") {
|
if (controllers[i]) {
|
||||||
m.render(root, modules[i].view(controllers[i]), mode === "all")
|
m.render(root, modules[i].view(controllers[i]), mode === "all")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -543,6 +543,13 @@ var m = (function app(window, undefined) {
|
||||||
pendingRequests = Math.max(pendingRequests - 1, 0);
|
pendingRequests = Math.max(pendingRequests - 1, 0);
|
||||||
if (pendingRequests === 0) m.redraw()
|
if (pendingRequests === 0) m.redraw()
|
||||||
};
|
};
|
||||||
|
var endFirstComputation = function() {
|
||||||
|
if (m.redraw.strategy() == "none") {
|
||||||
|
pendingRequests--
|
||||||
|
m.redraw.strategy("diff")
|
||||||
|
}
|
||||||
|
else m.endComputation();
|
||||||
|
}
|
||||||
|
|
||||||
m.withAttr = function(prop, withAttrCallback) {
|
m.withAttr = function(prop, withAttrCallback) {
|
||||||
return function(e) {
|
return function(e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue