Skip to content
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

components) Add afterRender #24

Closed
brianmhunt opened this issue May 17, 2017 · 5 comments
Closed

components) Add afterRender #24

brianmhunt opened this issue May 17, 2017 · 5 comments

Comments

@brianmhunt
Copy link
Member

Per knockout/knockout#1944

Noting comment by @jmvtrinidad on tko.computed issue (pre-monorepo):

Some of the useful callbacks from durandal. canActivate callback allows returning a promise then after promise is resolve, component will start binding or if it was reject, component will cancel rendering. I hope this helps designing callbacks.

@brianmhunt
Copy link
Member Author

The applyBindings* functions now return Promises, so it'd be trivial to add an afterBinding binding e.g.

class AfterBindingHandler extends ko.AsyncBindingHandler {
    constructor (params) {
      super(params)
      applyBindingsToDescendants(this.$context, this.$element)
        .then(this.completeBinding) // notify parent bindings that this binding is done
        .then(this.value) // this.value is the `valueAccessor()`, presumably a function.
    }

    get controlsDescendants () { return true }
}

Then, once registered as e.g. after with e.g. ko.bindingHandlers.set({after: AfterBindingHandler}), you'd use it like this:

<span data-bind='after: callback'></span>

where callback is some function.

I think this is a reasonable solution for the problem of callbacks being called when a component is done rendering. That said, it's not the same as afterRender.

I'd accept the equivalent of the PR from knockout/knockout#1944 for afterRender functionality.

Please share if you think there are other or better options!

@krnlde
Copy link
Contributor

krnlde commented Jun 2, 2017

Maybe do

constructor (...params) {
  super(...params)
  // ...
}

to make it even more future-proof.

@brianmhunt
Copy link
Member Author

The BindingHandler constructor API follows Crockford's single-object-param model, so we can extend it by adding named attributes to the param object that's passed in via applyBindings.

@krnlde
Copy link
Contributor

krnlde commented Jun 2, 2017

awesome!

@brianmhunt
Copy link
Member Author

Closing in lieu of #47 (just for ease of reference)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants