Comments (7)
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
on.click: window.href. ...
orclick: => 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.
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.
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.
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.
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.
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.
🍰!
from tko.
Related Issues (20)
- Deprecate unused npm packages HOT 2
- Convert legacy documentation
- Replace hard-coded parser unicode identifier detection with RegExp HOT 3
- optionsText with a lambda value fails to resolve the function argument HOT 2
- Add preventDefault to event handler options HOT 5
- Incorrect tree with multiple 'or' operators in parser HOT 1
- Unable to load from an ES module HOT 1
- TypeError: Cannot set properties of undefined (setting 'ko') HOT 1
- Re-enable tests on builds/knockout HOT 2
- `options.bindingGlobals` can't be changed at runtime
- Revamp www.tko.io
- Fix typing of the Observable<boolean> HOT 5
- Change mappingHelpers `objectLookup` from an O(n) array to O(1) Map
- Fix the build / exports in package.json
- Build @tko/build.reference into the `tko` package
- Server side render SSR support
- How to build and import TKO-beta as a bundled library. HOT 3
- [beta1.3] ESM support HOT 3
- Writable computed out of sync
- If you have multiple || (ORs), you need parentheses, otherwise it fails silently HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tko.