knockout / tko

🥊 Technical Knockout – The Monorepo for Knockout.js (4.0+)
http://www.tko.io
Other
275 stars 34 forks source link

components) Add `afterRender` #24

Closed brianmhunt closed 6 years ago

brianmhunt commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

Maybe do

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

to make it even more future-proof.

brianmhunt commented 7 years ago

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 commented 7 years ago

awesome!

brianmhunt commented 6 years ago

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