【发布时间】:2015-07-17 13:25:34
【问题描述】:
我正在编写backbone.js 中的代码,并且尝试一次获取15 个项目的分页时遇到问题。
型号:
window.Quote = Backbone.Model.extend({});
收藏:
window.Quotes = Backbone.Collection.extend({
model: Quote,
url: 'https://gist.githubusercontent.com/anonymous/8f61a8733ed7fa41c4ea/raw/1e90fd2741bb6310582e3822f59927eb535f6c73/quotes.json'
});
单引号视图:
window.QuoteEntryView = Backbone.View.extend({
tagName: "tr",
template: _.template('<td><%- source %></td> <td><%- quote %></td> <td><%- theme %></td>'),
render: function() {
return this.$el.html(this.template(this.model.attributes));
}
});
所有报价视图:
window.QuoteListView = Backbone.View.extend({
tagName: "table",
id: "myTable",
className: "tablesorter",
initialize: function() {
this.collection.on('change', this.render, this);
this.render();
},
render: function() {
this.$el.children().detach();
this.$el.html('<th>Source</th> <th>Quote</th> <th>Theme</th>').append(
this.collection.map(function(quote){
return new QuoteEntryView({ model: quote }).render();
})
);
}
});
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"> </script>
<link rel="stylesheet" type="text/css" href="./styles/myStyle.css">
</head>
<body>
<div id="main-view"></div>
<script src="./models/quotesModel.js"></script>
<script src="./views/singleQuotesView.js"></script>
<script src="./views/allQuotesView.js"></script>
<script src="./collections/allQuotes.js"></script>
<script>
$(function() {
var postFetchBoot = function(collection) {
$('#main-view').append(new QuoteListView({
collection: collection
}).$el);
};
new Quotes().fetch({
success: postFetchBoot
});
});
</script>
</body>
</html>
我已经包含了我的整个代码。我曾尝试使用“骨干分页器”,但在尝试将其附加到我的代码时遇到了麻烦。如果有人可以帮忙,请谢谢!
【问题讨论】:
-
你试过用 Backbone.PageableCollection 代替 Backbone.Collection 吗?
-
@Molda,我确实尝试过,但是我找不到任何好的示例来了解如何实现所有内容,还需要使用 backgrid.js 吗?
标签: javascript jquery html backbone.js pagination