繁体   English   中英

骨干视图多次触发事件委托

[英]backbone view is triggering the event delegate many times

嗨,我对骨干网有类似的看法

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时,在我的视图中调用了download方法,该方法应该从ajax调用中选择一个URL,然后添加到LI下的anchot标签的href属性中然后触发锚点单击,但是当我执行该操作后,无限期地开始在循环中调用下载方法,请问如何才能告诉我为什么会发生这种情况? 谢谢!!!!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM