【发布时间】:2014-09-29 22:13:31
【问题描述】:
您好,所以我在骨干网中有一个类似于此的视图
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 下,然后我触发了锚点单击,但是当我这样做时,下载方法开始无限期地循环调用,有人能告诉我为什么会这样吗?谢谢!!!!
【问题讨论】:
-
您应该考虑将模板移至 DOM。
标签: javascript jquery events backbone.js views