Mithril.js: Problem with m.request and m.redraw

Created on 15 Jun 2015  Â·  5Comments  Â·  Source: MithrilJS/mithril.js

Trying to use m.request with background: true as described at the end of this post or in the API guide here resulted in some errors as my view was not being redrawn.

As you will see in this example , this pattern in controller doesn't work:
ctrl.data = example.getQuotes(ctrl.ticker()).then(m.redraw)
I can see from the log that i get the data once the promise is resolved, however it does not trigger a redraw!

however this works:

  ctrl.data = example.getQuotes(ctrl.ticker());
    ctrl.data.then(m.redraw);

and my view is redrawn with the correct data, when the m.request promise is resolved.
Is that a bug with m.redraw or should we amend the blog post and API accordingly?

Cheers

Most helpful comment

Closing. You're welcome, @barneycarroll @hugufc. :smile:

All 5 comments

A few test cases (please ignore the large amount of cruft in the m.request options – this is an attempt to get Mithril's XHR API to produce something jsFiddle's API will respond to). It appears that:

  1. Mithril's thennables will not assign undefined (the return value of m.redraw) to the underlying m.prop, leaving the initialValue in place as the value – while still passing whatever through the promise chain (see logs before and after then( m.redraw ): http://jsfiddle.net/barney/baejsn9x/
  2. It's less ambiguous (to my eyes) to keep separate promise chains when a promise is both a queryable m.prop and a hook for callbacks (ie side-effects): http://jsfiddle.net/barney/zu11jrgc/
  3. …but in theory you should be able to do both by chaining a callback which executes side-effects while ensuring the promise value is passed through: http://jsfiddle.net/barney/dn4vqq66/

So 1 is confusing but explicable given that Mithril is munging m.prop & promise into a single API. 2 is something I do all over the place, but the pattern isn't obvious. The last snippet in this blog post [http://lhorie.github.io/mithril-blog/wait-for-it.html] should change unless we patch Mithril to behave differently. 3 definitely feels like a bug to me – I don't understand why it doesn't work.

Part of me thinks there's too much magic going on with this API anyway, and we should pull documentation to explicitly advocate the pattern in 2 (chained callbacks in m.props should be used exclusively to transform the underlying value).

…another part of me thinks we could add more magic such that @Bondifrench's first example above just works: fix the bug in fiddle 3, and tweak m.redraw such that it returns whatever was passed in.

tweak m.redraw such that it returns whatever was passed in.
:+1:

This would solve so many conundrums I fall into every day.

This issue can not be closed?

Good idea @hugufc. @Bondifrench would need to reopen if we were to remember exactly what we were thinking all those months ago :)

Closing. You're welcome, @barneycarroll @hugufc. :smile:

Was this page helpful?
0 / 5 - 0 ratings