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

template binding's afterRender handler does not get invoked on DOM render #30

Closed
vikashpisces opened this issue Jun 7, 2017 · 9 comments

Comments

@vikashpisces
Copy link

I am trying out TKO 4.0 in ES6 semantics with transpilation done using Babel.

When I use template binding and pass a class method to afterRender, the method does get invoked but the reference of this becomes window Object. However, if the same class method is added to click binding, method gets invoked and there this reference points out to class or the viewmodel.

Not sure if this is an issue with TKO 4.0 or ES6 compilations. If someone can help me on this with a sample working example of afterRender using ES6 and TKO 4.0 would be of great help at this time. As we have planned for migration from KO to TKO for good performance reason.

I am attaching my tried code zipped below:

Thanks is advance.

side-bar.zip

@krnlde
Copy link
Contributor

krnlde commented Jun 7, 2017

You could try either the autobind feature from core-decorators or instead use an arrow function to preserve the this context

@brianmhunt
Copy link
Member

@vikashpisces It would help to see a jsFiddle.

@vikashpisces
Copy link
Author

@brianmhunt I have a jsFiddle created which illustrates the problem.

In the jsFiddle, you can see error in console when afterRender callback of template binding is being invoked on DOM render.
The same works with click binding (button element is commented out in the jsfiddle provided).
It will be nice of you if you can provide a solution for this with a brief description on what is going wrong in my code.

Thanks in advance.

@brianmhunt
Copy link
Member

@vikashpisces The first issue is that you're using Knockout 3.4 in the jsFiddle.

You can use the TKO library (i.e. KO4 alpha) here: https://unpkg.com/tko@4.0.0-alpha2/dist/ko.js

This version works: https://jsfiddle.net/bmh_ca/q0hh2vta/3/

Let me know if there's an issue .

(Sorry for short comment -- got to run!)

@brianmhunt
Copy link
Member

Just to complete my prior comment - You need to use $data.afterRender in order to get the bound version of afterRender. This is because the afterRender function might be found on $data, $context, the globals or an alias (e.g. foreach ... as:).

If there is anything we do to improve this, please share and we can re-open.

@vikashpisces
Copy link
Author

@brianmhunt Thanks a lot for the quick help on this. Binding renderhandler to $data or using fat arrow worked for me.

One thing I would like to highlight that $data was always default current context. Whether to mention it or not, binding's current context was $data. I am not sure what has changed in that context. It could be the ES6 compiler which is causing this issue.

If any support from KO provided on this part can help people who are migrating to ES6. Otherwise every places default context need to be mentioned explicitly.

Thanks @brianmhunt & @krnlde for your time.

@brianmhunt
Copy link
Member

This issue did occur in 3.4, as you can see from the first fiddle ... but I really don't know the rationale... @mbest - any idea why afterRender is being passed null as this, instead of $data?

@mbest
Copy link
Member

mbest commented Jun 9, 2017

I think the rationale is that afterRender (and afterAdd, etc.) are meant for UI manipulation mostly and so aren't especially meant to manipulate the model.

@brianmhunt
Copy link
Member

Got it thanks @mbest.

I think with ES6 classes being used as view models the expectation has probably changed. (As per this issue)

There's no technical reason we can't do this, is there?

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

4 participants