Comments (13)
@davidrus Seems like you're adding parens to the decorator, which is maybe causing the issue.
It should look like:
import { Component, h, define } from 'skatejs';
import { bind } from 'decko';
export default class App extends Component<void> {
static get is() { return 'my-app'; }
@bind
handleClick() {
console.log( 'clicked with this scope: ', this );
}
renderCallback() {
return [
<div onClick={this.handleClick}>Hello my lord!</div>,
];
}
};
define(App);
from decko.
Thank you for responce, but I tried it and still the same error.
I created codepen for it, so you can test it: http://codepen.io/davidrus/pen/vxXobW
from decko.
Hey folks! so it turns out problem is somewhere between skate and web-components polyfill.
With vanilla js decko works without problems
Demo:
http://codepen.io/Hotell/pen/bqwXmm?editors=1010
from decko.
Gif or didn't happen right? :D
from decko.
all right so after further investigation the issue is definitely with web component polyfills + skate.
cc @treshugart @azakus @justinfagnani
It can be fixed though on decko's side.
What's causing the trouble is getter in which you are overriding descriptor ( ie calls it until it explodes )
what can be done:
- redefine descriptor only with getter http://codepen.io/Hotell/pen/wJoaba?editors=1010
- make surer that original getter is invoked only once http://codepen.io/Hotell/pen/EWNaJy
check myBind
function ( that's deckos bind redefined )
from decko.
Hmm - that breaks caching though - the closure in bind()
is scoped to the class definition, whereas get()
is scoped to the instance.
I wonder though, why this is only being triggered from skate - maybe this method is getting into an infinite loop because of the reassignment?
from decko.
@Hotell I don't see how this is a problem with Skate.
To get around this, you could use http://babeljs.io/docs/plugins/transform-class-properties/ and just do:
handleClick = () => {
// this is now what you expect without explicit binding
}
This has, at least, seemed to work in IE for me, but maybe that's changed with the polyfill?
from decko.
@treshugart right, just we're talking about @bind
as an alternative means of doing that. Internally it's just this:
function bind(target, key, desc) {
let fn = desc.value;
return {
configurable: true,
get() {
// only bind to each instance on first access, then cache be replacing property descriptor
let bound = fn.bind(this);
Object.defineProperty(this, key, {
value: bound
});
return bound;
}
};
}
class Foo extends Component {
@bind
handleClick(e) {
this instanceof Foo; // true
}
render() {
return <div onClick={this.handleClick} />
// ^ binds here only on first render
}
}
I checked and it seems like MDN actually recommends assigning a property instead of redefining it. Perhaps decko could switch to doing that.
from decko.
@Hotell I don't see how this is a problem with Skate.
right sorry, wrong wording ( I didn't meant it's explicitly Skates fault ), but it's a possible culprit
To get around this, you could use http://babeljs.io/docs/plugins/transform-class-properties/ and just do...
right it has one problem though:
if somebody does this ( I know within solid team with solid style guides this wont happen ever :) )
class Foo {
greeting = this.sayWat()
sayWat = () => { return 'WAT' }
}
I checked and it seems like MDN actually recommends assigning a property instead of redefining it. Perhaps decko could switch to doing that.
Hmm interesting, didn't know about this "best practice" I personaly try to avoid delete
but looks like it could be useful in here
from decko.
I'm going to try out the simpler overwrite approach from that MDN page and we can check if that fixes the repro :)
from decko.
@developit the solution can be found here https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js#L68-L71
from decko.
Nice - will have to add that lock.
from decko.
@developit please take a look #16
from decko.
Related Issues (10)
- [DOCS] Better options documentaion HOT 1
- Non-primitive function parameters HOT 7
- TS definitions HOT 4
- @debounce is named improperly — should be @throttle HOT 4
- @bind decorator bug when super method is called HOT 5
- Don't think @memorize fit any case. HOT 1
- Is memoize supposed to work on getters? HOT 2
- Change description to avoid "ES7"
- Issues with Typescript HOT 1
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 decko.