javascript - 主干 View 多次触发事件委托(delegate)

标签 javascript jquery events backbone.js view

嗨,我的 Backbone View 与此类似

app.FileListItemView = Backbone.View.extend({
    tagName: 'li',
    className: 'list-group-item clearfix',
    events: {
        'click .download-action': 'download'
    },
    template: _.template(
            "<div class='pull-left' style='width: 80%'>\n\
                <%= name %><br><span style='font-size: 10px;'><%= path %></span>\n\
            </div>\n\
            <div class='pull-right' style='width: 20%'>\n\
                <div class='dropdown'>\n\
                    <button class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'>\n\
                      Actions\n\
                      <span class='caret'></span>\n\
                    </button>\n\
                    <ul class='dropdown-menu' role='menu'>\n\
                      <li class='download-action' data-id='<%= id %>' data-accountid='<%= accountid %>' role='presentation'><a class='super-anchor' role='menuitem' tabindex='-1' href='#'>Download</a></li>\n\
                    </ul>\n\
                </div>\n\
            </div>"
            ),
    initialize: function () {
    },
    render: function () {
        this.$el.html(this.template(this.model));
        return this;
    },
    download: function (event) {
        //console.log(this.$el.find('.download-action-link').data('id'));
        var el = this.$el.find('.download-action');
        var id = el.data('id');
        var accountid = el.data('accountid');
        var url = app.config.get('apiURL');
        var apiKey = app.config.get('apiKey');

        console.log(el.prop('tagName'));

        $.ajax(url + '/accounts/' + accountid + '/links', {
            data: {
                'file_id': id, 'direct': true
            },
            headers: {
                Authorization: 'ApiKey ' + apiKey
            },
            type: 'POST',
            success: function (data) {
                if (data.active) {
                    var a = el.find('.super-anchor');
                    console.log(a);
                    //change this part
                    //a.attr('href', data.url).trigger('click');
                    //to this
                    a.attr('href', data.url).on('click', function(event){
                      event.preventDefault();
                      event.stopPropagation();
                    })[0].click();
                }
            }
        });
    }
});

这个想法是,当我点击“download-action”类的 LI 时,在我看来,下载方法会被调用,应该从 ajax 调用中选择一个 url,然后添加到 anchot 标签的 href attr 中,即在 LI 下,之后我触发 anchor 单击,但是当我这样做时,下载方法开始无限期地循环调用,有人可以告诉我为什么会发生这种情况吗?谢谢!!!!

最佳答案

我假设触发的点击事件正在传播到 li.dow​​nload-action,然后再次启动 ajax 下载功能。解决方案是使用 window.location.href 将用户导航到新 URL 或将该链接移出 li.dow​​nload-action 之外。

关于javascript - 主干 View 多次触发事件委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109758/

相关文章:

javascript - 未加载谷歌地图回调?

javascript - 使用 jQuery 附加时,引号没有按预期显示

javascript - 有人能发现问题吗?只有第一个选项适用于选择

javascript - 通过覆盖转发和嗅探事件

javascript - Controller <-> 服务交互和单元测试 : Am I doing this wrong?

javascript - AngularJS - ng-repeat 生成的 Controller 实例内的事件问题

JavaScript 对象问题

javascript - 在链接上悬停显示 div,在该 div 和链接悬停时显示

javascript - 这可能通过 CSS3 实现吗?或者我需要javascript吗?

javascript - jQuery:通过 css-selector 注册的事件的优先级?