Reputation: 790
Hi so I have a view in backbone similar to this
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();
}
}
});
}
});
the idea is that when I click the LI with "download-action" class the download method is called in my view, that should pick a url from the ajax call and then added to the href attr of the anchot tag which is under the LI and after that I trigger the anchor click, but when I do that the download method start to be called in loop indefinitely, can some how tell me why is this happening?? thanks!!!!
Upvotes: 0
Views: 90
Reputation: 99
I assume the triggered click event is being propagated up to li.download-action which then starts the ajax download function all over again. A solution would be to use window.location.href to navigate the user to the new url or to move that link out side li.download-action.
Upvotes: 1