2016-10-20 9 views
0

サードパーティのlib(fabricjs)のイベントハンドラから私のポリマー要素のプロパティをどのようにして.set()のプロパティを見つけることができません。たとえば、次のようにポリマーイベントハンドラのスコープから要素のプロパティを設定する方法

<dom-module id="x-example"> 
    <template> 
    <div id="objectTools" hidden$="{{objectToolsHidden}}"> 
     <h1> object toolz!</h1> 
    </div> 
    <canvas id="c" width="400" height="400"></canvas> 
    </template> 
    <script> 
    Polymer({ 
    is: 'x-example', 
    properties: { 
     canvas: Object, 
     objectToolsHidden: { 
     type: Boolean, 
     notify: true, 
     value: true 
     } 
    }, 
    _onObjectSelected: function(e){ 
     //hmm, this is the canvas elem... (???) 
     this.set('objectToolsHidden', false); 
    }, 
    _onSelectionCleared: function(e){ 
     this.set('objectToolsHidden', true); 
    }, 
    ready: function(){ 
     this.canvas = new fabric.Canvas(this.$.c); 
     var rect = new fabric.Rect({ 
      top : 100, 
      left : 100, 
      width : 60, 
      height : 70, 
      fill : 'black' 
     }); 

     this.canvas.add(rect); 

     this.canvas.on({ 
     'object:selected': this._onObjectSelected, 
     'selection:cleared': this._onSelectionCleared 
     }); 
    } 
    }); 
    </script> 
</dom-module> 

...呼び出されますが、this.set()を関数として定義されていません_onObjectSelected方法。スコープイベントハンドラの内側から要素プロパティにアクセスするにはどうすればよいですか?

も、jsfiddle:http://jsfiddle.net/ysoo8mjg/

答えて

0

ああ!私は、イベントハンドラ登録

this.canvas.on({ 
    'object:selected': this._onObjectSelected.bind(this), 
    'selection:cleared': this._onSelectionCleared.bind(this) 
    }); 

https://jsfiddle.net/edwardsharp/9foteodx/8/

.bind(this)を使用することができます
関連する問題