2016-10-04 4 views
0

d3.drag機能を備えた次のバックボーンモデルがあります。モデルのthisをd3のコンテキスト内で呼び出すことはできません。backbone.modelをd3.dragコンテキスト内で呼び出す方法

変数model=thisを定義してmodel.draw..を呼び出すことで、同様の質問のソリューションを見つけましたが、どのようにd3のドラッグ内に追加できますか?

DataMapper.Models.Anchor = Backbone.Model.extend({ 
       defaults: { 
         //... 
       }, 
       initialize : function(){ 
         d3.select("#anchor").call(this.dragAnchor); //make the #anchor draggable 
       }, 
       dragAnchor: d3.drag() 
         .on("start", function (d) { 
          console.log("something"); //it prints 
          var thisDragY = this.drawSomething(a,b,c); 
          // this.drawSomething is not a function 
          // because inside d3.drag(), 'this' refers to #anchor 
          // what I want to refer is the model 
         }) 
         .on("drag", function (d) {}) 
         .on("end", function (d) {}), 
       drawSomething: function (parent, cx, cy) { 
        //code 
       } 
      }); 

目的の達成にアンダースコアのbindを使用する方法はありますか? Link to a useful article

答えて

0

解決策はチームメンバーによって検出されました。ドラッグを関数として呼び出すことができました。

DataMapper.Models.Anchor = Backbone.Model.extend({ 
       defaults: { 
         //... 
       }, 
       initialize : function(){ 
         d3.select("#anchor").call(this.dragAnchor()); //make the #anchor draggable 
       }, 
       dragAnchor: function(){ 
         var self=this; 
         return d3.drag() 
          .on("start", function (d) { 
           console.log("something"); //it prints 
           var thisDragY = self.drawSomething(a,b,c); 
          }) 
          .on("drag", function (d) {}) 
          .on("end", function (d) {}), 
       drawSomething: function (parent, cx, cy) { 
        //code 
       } 
      }); 
関連する問題