var MyView = Backbone.View.extend({
template: $('#my-view-template').html(),
render: function (source) {
// compile the Underscore.js template
var compiledTemplate = _.template(this.template);
// render the template with the model data
var data = _.clone(this.model.attributes);
var html = compiledTemplate(data);
this.template.$(".aVideo")
this.$aVideo = this.$('#aVideo');
this.$aVideo.attr("href", source);
this.$el.append(html);
}
});
<script type="text/html" id="my-view-template">
<div style="border:1px solid firebrick; padding:15px; color:firebrick">
<a class="btn btn-link" id="aVideo" target="_blank" style="font-size:15px;">View Trailer</a>
</div>
</div>
</script>
$.ajax({
url: '/Movie/Movies',
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function (data) {
for (var i = 0; i < data.length; i++) {
var movie1 = new Movie({
"title": data[i].TITLE,
"description": data[i].DESCRIPTION,
"storyline": data[i].STORYLINE,
"rating": data[i].RATING,
"infobar": data[i].INFOBAR,
"director": data[i].DIRECTOR,
"stars": data[i].STARS
});
var myView = new MyView({
model: movie1
});
myView.setElement("#content");
myView.render(data[i].VIDEO);
}
},
error: function (xhr) {
alert('error');
}
});
每个视频都会被渲染为最后一个视频预告片。 我不明白为什么我要收到每部电影的最后一个视频
答案 0 :(得分:-1)
我找到了解决问题的另一种方法。 THX!
<a class="btn btn-primary" id="aVideo" target="_blank" href="<%= video %>" style="font-size:15px;">View Trailer</a>