-
Notifications
You must be signed in to change notification settings - Fork 479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prioritize resolution by .then
for thenable functions
#735
Comments
isThenable
(slightly).then
for thenable functions
I just noted at least one other place where a small change is required, namely |
Interesting, thanks so much for the thorough overview. I see you're working on a PR; I'm totally on-board with pulling this in when you're ready. |
As an aside, how does one use Knockout with Dust, anyways? I would have assumed that Knockout handled all the templating. |
Awesome. Thank you so much for receiving and considering the PR, which is now up as #736.
Knockout is essentially template-agnostic, but it's much easier to stick with the default templating system. The data/observable model, however, can be completely severed from Knockout.† † I am in the process of breaking Knockout out into reusable parts now (per, among others, tko.observable). |
Here are a couple simple changes to make function-thenables work as expected and increase compatibility with e.g. Knockout:
1. Change
dust.isThenable
toThis matches Promises A+ spec, which states "1.2 “thenable” is an object or function that defines a then method."
(As a total aside, you may want to test that
elem !== null
sincetypeof null
isobject
andnull.then
will throw an exception.)2. Towards the end of
Context.prototype._get
change:if (typeof ctx === 'function') {
toif (typeof ctx === 'function' && !dust.isThenable(ctx)) {
So if a "thenable function" (a function with a
.then
) is passed in, it prioritizes resolution by via the.then
over calling the function.3. In
Chunk.prototype.reference
move:to the beginning of the function (i.e. before
if (typeof elem === 'function') {
).4. Add a test, along these lines (pardon the style/naming):
Use case
If you are using Knockout.js (and lots of folks do to) then most view-models passed in will use knockout observables, and look like this:
These observables are functions; you call them to read the value, write to them by passing the new value as the argument to the same function i.e.
x(123)
sets the value to123
;x()
then returns123
.When you call dust with this with e.g.
dust.renderSource("{x}", vm, ...)
it modifies the observable, which is undesirable. (It becomes thechunk
viactx.apply(ctxThis, arguments);
inContext.prototype._get
.)Prioritizing
thenable
over functions makes it trivial for Knockout to expose its value. (For interests sake, until it's built in, KO users need to add something like this:ko.subscribable.fn.then = function(res) { res(this()) }
). I also feel like this is the more natural prioritization.I doubt many folks will have thenable functions, so I suspect this change has a low probability of breakage – and where folks do have thenable functions I suspect the preference is to resolve via the
then
as opposed to a function call.Thanks for reading; I hope this proves to be a useful request. Happy to put together a PR, but I just posted here since the changes seem pretty simple.
Cheers.
The text was updated successfully, but these errors were encountered: