我有一个简单的页面,其中显示了书籍列表以及有关书籍、标题、价格、描述的一些详细信息。数据是从 JSON 文件中提取的。
当我点击列出的任何一本书时,一个灯箱(引导模式)会启动,我想在其中显示被点击的书名。用户将能够写评论,所以我也想获取然后发送图书 ID。
不确定从被点击的书中获取数据的最佳方式是什么?
到目前为止,这是我的代码(包括灯箱):
骨干:
var Book = Backbone.Model.extend();
var BookList = Backbone.Collection.extend({
model: Book,
url: 'json/books.json'
});
var BookView = Backbone.View.extend({
el: '.booksList',
template: _.template($('#booksTemplate').html()),
render: function(){
_.each(this.model.models, function(model){
this.$el.append(this.template({
data: model.toJSON()
}));
}, this);
return this;
}
});
var AppView = Backbone.View.extend({
el: 'body',
initialize: function(){
var bookList = new BookList();
var bookView = new BookView({
model: bookList
});
bookList.bind('reset', function(){
bookView.render();
});
bookList.fetch();
}
});
var appView = new AppView();
模板:
<script id="booksTemplate" type="text/template">
<div class="book">
<div class="bookDetails">
<h3><%= data.title %></h3>
<p><%= data.price %></p>
</div>
<p><%= data.description %></p>
<a href="#myModal" data-toggle="modal">bid</a>
</div>
<div id="myModal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3><%= data.title %></h3>
</div>
<div class="modal-body">
<form action="#" method="post">
<input type="text" name="comment" id="comment" />
</form>
</div>
<div class="modal-footer">
<a href="#" class="btn close" data-dismiss="modal" aria-hidden="true">Close</a>
</div>
</div>
</script>