2016-10-04 20 views
0

我有以下骨幹模型與d3.drag功能。我不能在d3的上下文中調用模型的this如何調用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 
       } 
      }); 
相關問題