Giter Site home page Giter Site logo

Comments (9)

krnlde avatar krnlde commented on May 22, 2024 1

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

from tko.

brianmhunt avatar brianmhunt commented on May 22, 2024 1

@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/[email protected]/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!)

from tko.

brianmhunt avatar brianmhunt commented on May 22, 2024 1

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.

from tko.

brianmhunt avatar brianmhunt commented on May 22, 2024

@vikashpisces It would help to see a jsFiddle.

from tko.

vikashpisces avatar vikashpisces commented on May 22, 2024

@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.

from tko.

vikashpisces avatar vikashpisces commented on May 22, 2024

@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.

from tko.

brianmhunt avatar brianmhunt commented on May 22, 2024

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?

from tko.

mbest avatar mbest commented on May 22, 2024

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.

from tko.

brianmhunt avatar brianmhunt commented on May 22, 2024

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?

from tko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.