如何引发Backbone.js的意见/绑定自定义事件? [英] How to trigger / bind custom events in Backbone.js views?

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

问题描述

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

iScroll出版的 onScrollEnd 事件,但我似乎无法绑定/订阅它的视图中:

  App.Views.Scroller = Backbone.View.extend({    事件:{
        onScrollEnd':'scrollEnd
    },    初始化:功能(){
        VAR自我=这一点;
        this.scroller =新iScroll(内容滚动',{
            onScrollEnd:功能(){
                self.trigger('onScrollEnd');
            }
        });
    },    scrollEnd:功能(E){
        //从来没有所谓:(
        的console.log(E);
    }});


解决方案

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

但你触发了您的视图对象,而不是元素的onScrollend事件。

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

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

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);
    }

});

解决方案

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.

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

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

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

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