Comments (9)
You could try either the autobind feature from core-decorators or instead use an arrow function to preserve the this
context
from tko.
@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.
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.
@vikashpisces It would help to see a jsFiddle.
from tko.
@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.
@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.
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.
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.
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)
- 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.