如何在Backbone.js视图中触发/绑定自定义事件? [英] How to trigger / bind custom events in Backbone.js views?

查看:174
本文介绍了如何在Backbone.js视图中触发/绑定自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主干视图,它使用 iScroll 实现幻灯片。

I have a Backbone View that uses iScroll to implement a slideshow.

iScroll发布一个 onScrollEnd 事件,但我似乎无法在视图内绑定/订阅: p>

iScroll publishes an onScrollEnd event, but I cannot seem to bind/subscribe to it inside the View:

App.Views.Scroller = Backbone.View.extend({

    events: {
        'onScrollEnd' : 'scrollEnd'
    },

    initialize: function(){
        var self = this;
        this.scroller = new iScroll('content-scroller', {
            onScrollEnd: function() {  
                self.trigger('onScrollEnd');
            }
        });     
    },

    scrollEnd: function(e){
        // never called :(
        console.log(e);
    }

});


推荐答案

您的onScrollEnd事件绑定到视图的顶部元素; scrollEnd将当视图的HTML元素接收到onScrollEnd事件时调用。

Your onScrollEnd event is bound to the view's top element; scrollEnd will be called when the view's HTML element received an onScrollEnd event.

但是您触发了View对象上的onScrollend事件而不是元素。

But you are triggering an onScrollend event on your View object, not the element.

所以你可能想说 $(self.el).trigger('onScrollEnd'); ,或者直接调用函数: self.scrollEnd()

So you probably want to say $(self.el).trigger('onScrollEnd'); instead, or perhaps call the function directly: self.scrollEnd().

这篇关于如何在Backbone.js视图中触发/绑定自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆