【问题标题】:Draggabilly.js events in the Backbone.js viewBackbone.js 视图中的 Draggabilly.js 事件
【发布时间】:2013-11-19 08:58:42
【问题描述】:

我在 Backbone 视图中使用 Draggabilly.js,如下所示:

var appView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },
    render: function(){
     var draggie = new Draggabilly(this.el);
     draggie.on('dragEnd', this.updateModel); //This works by I can access the this.model
    },
    events: {
      'dragEnd':'updateModel' //This doesn't work
    },
    updateModel: function(instance, event, pointer){
      //Here I want to update my model bassed on the pointer's x and y
      //However I can't access this.model
    }
)};

Draggabilly 在拖动结束时提供事件'dragEnd'。不过,我可以将它作为一个事件集成到我的 Backbone 视图中。

你能指点一下吗?

【问题讨论】:

    标签: javascript jquery backbone.js javascript-events


    【解决方案1】:

    为了访问View 上下文,您可以这样做:

    draggie.on('dragEnd', _.bind( this.updateModel, this ));
    

    但是,如果您想稍后删除 dragEnd 的侦听器,在初始化代码中使用 bindAll 可能会很有用:

    initialize: function() {
       _.bindAll(this, 'updateModel');
    }
    

    查看 Backbone 文档以获取更多 info。

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多