我一直在努力骨幹。但我要鎖定收集和查看。渲染Backbone.js的集合與模板
的JavaScript在這裏與一個模型,一個集合,一個觀點:
var lieu = Backbone.Model.extend({
defaults: {
Lieu: '',
Activite: '',
ImagePrincipal: '',
Gallery:'',
Texte:''
}
});
var LieuCollection = Backbone.Collection.extend({
model : lieu
});
var LieuView = Backbone.View.extend({
el: '.slider',
initialize: function() {
this.template = _.template($("#lieu_template").html());
this.collection.bind("reset", this.render, this);
},
render: function() {
var Content = this.template(this.collection.toJSON());
$(this.el).html(Content);
return this;
}
});
$(function() {
var Collection = new LieuCollection();
Collection.add(
new lieu({
Lieu: 'Lorem 1',
Activite: 'Lorem 2',
ImagePrincipal: 'http://lorempixel.com/g/1200/800/sports/',
Gallery: [
'http://lorempixel.com/g/1200/800/AAA/',
'http://lorempixel.com/g/1200/800/BBB/',
'http://lorempixel.com/g/1200/800/BBB/',
'http://lorempixel.com/g/1200/800/BBB/'
],
Texte: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam gravida purus eros, id tempus elit ullamcorper vitae. Fusce faucibus velit diam, vitae pulvinar dui convallis eu. In non sem sit amet odio lobortis dignissim id a nisl. Nulla ut metus elementum, iaculis lacus non, laoreet risus. Aliquam placerat tempus dapibus. Nam ut tristique odio, sed consequat justo. Integer id nislat ante ultrices dictum.'
})
);
var lieuView = new LieuView({collection: LieuCollection});
});
與模板的HTML,它的確定? :
<section id="events" role="events" data-id="events" class="line w100 center">
<div class="eventSlider">
<div class="slider">
<script type="text/template" id="lieu_template">
<% _.each(lieux, function(lieu) { %>
<div class="item">
<div class="intro">
<div class="midway-horizontal midway-vertical">
<h1><%= lieu.Lieu %></h1>
<p><%= lieu.Activite %></p>
</div>
</div>
<img src= "<%= lieu.ImagePrincipal %>" />
</div>
<% }); %>
</script>
</div>
</div>
</section>
非常感謝。文檔骨幹是偉大的,但我不知道爲什麼它不工作...
究竟是什麼不起作用?你在期待什麼,你究竟看到了什麼? – Hazaart