GithubHelp home page GithubHelp logo

Error with .getbindingAccessors about tko HOT 7 CLOSED

knockout avatar knockout commented on July 19, 2024
Error with .getbindingAccessors

from tko.

Comments (7)

brianmhunt avatar brianmhunt commented on July 19, 2024

Awesome!

Unfortunately we don't have a native JavaScript compiler anymore - so some code in data-bind strings will not work.

Fortunately the workarounds are easy in this case. You can change the binding to

  1. on.click: window.href. ... or
  2. click: => window.href. ...

Ie change the function to an "on" event binding, or to a lambda. (Incidentally the above options should be identical - except the on.click option is backwards-compatible with ko 3.x and punches).

Sorry the parser in tko doesn't handle the function call - it's definitely one of the legacy issues that would be nice to have a work around for.

I will think about it for the next alpha since I think this will be a common issue.

from tko.

jlaustill avatar jlaustill commented on July 19, 2024

As time has gone on, I have actually started second guessing how to use these bindings. I've started moving them all to functions internal to my viewmodels, like this

<li data-bind="click: goToSearch"></li>

and moving the code to my viewmodel like

var goToSearch = function () {
    window.location.href = '/#/Forum/Details?terms=' + encodeURIComponent(appVM.terms.peek());
}

Would this work in tko? Or would I still need to use the on.click or the arrow function? I'll play with it and see what I come up with. I'm not a fan of putting javascript like this in the dom anymore at all. I wrote this code when I was first figuring knockout out.

from tko.

brianmhunt avatar brianmhunt commented on July 19, 2024

Yeah tko should work exactly like ko for the function references on the view models (or binding context or options.globals/window)

The hope is that tko will encourage moving away from (complex) code in the dom.

from tko.

jlaustill avatar jlaustill commented on July 19, 2024

So this worked for this binding, then I ran into another one where I need to pass in a parameter. Basically, it's a paging binding that adds first, back, 1, 2, 3, 4, 5, next, last buttons. My binding looks like this

data-bind="click: function () {goToPage(pg)}"

and that pg is simply a number. How in TKO should I pass in that paramter if I can't use a function literal? Will it support the .bind syntax? Or is there a totally new way to go about it? This has actually been one of the most frustrating parts of ko since I started using it. It seems super common to build lists with a foreach and want to call a function with either an index value or a value from the object, such as userId, or postId, messageId, etc etc. I'm all up for a better way to handle these...

from tko.

brianmhunt avatar brianmhunt commented on July 19, 2024

Try this:

data-bind="click: => goToPage(pg)"

Tko supports "naked lambdas", being the ES6 lambda syntax of () => /* declaration */, but without the arguments i.e. => /* declaration */.

from tko.

jlaustill avatar jlaustill commented on July 19, 2024

Ok, I like this syntax! If it's one of the only non backward compatible issues, I think I could live with it. It only took me about 15 minutes to go through my entire intranet and search and replace all of these. And boy does it look cleaner!

I think that closes this issue, onto new ones :)

from tko.

brianmhunt avatar brianmhunt commented on July 19, 2024

🍰!

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.