Giter Site home page Giter Site logo

Comments (13)

developit avatar developit commented on September 10, 2024

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

davidrus avatar davidrus commented on September 10, 2024

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.

Hotell avatar Hotell commented on September 10, 2024

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.

Hotell avatar Hotell commented on September 10, 2024

Gif or didn't happen right? :D
decko-bind-ie11

from decko.

Hotell avatar Hotell commented on September 10, 2024

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 )

image

what can be done:

check myBind function ( that's deckos bind redefined )

and ofc gif:
decko-bind-ie11 wc-fix

from decko.

developit avatar developit commented on September 10, 2024

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.

treshugart avatar treshugart commented on September 10, 2024

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

developit avatar developit commented on September 10, 2024

@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 avatar Hotell commented on September 10, 2024

@treshugart

@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' }
}

@developit

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.

developit avatar developit commented on September 10, 2024

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.

finico avatar finico commented on September 10, 2024

@developit the solution can be found here https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js#L68-L71

from decko.

developit avatar developit commented on September 10, 2024

Nice - will have to add that lock.

from decko.

cronon avatar cronon commented on September 10, 2024

@developit please take a look #16

from decko.

Related Issues (10)

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.