我正在努力使用_.groupBy
功能。使用此代码,我在浏览器中呈现了这样的代码:
align
spellcheck
isContentEditable
contentEditable
Item 1
Item 1
Item 1
Item 1
Item 1
Item 1
Item 1
outerText
outerHTML
以下是我的全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Backbone test</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
ul.items{list-style: none;width: 100%;margin: 0px;-webkit-margin-before: 0em;-webkit-padding-start: 0px;}
</style>
</head>
<body>
<header>
</header>
<content>
<div class="jumbotron">
<div class="container">
<h1>My Items!</h1>
</div>
</div>
<div id="items"></div>
<div class="jumbotron">
<div class="container">
<h1>My Grouped Items?</h1>
</div>
</div>
</content>
<footer>
</footer>
<script id="allItemTemlate" type="text/template">
<ul>
<% _.each( items, function( category, i ){ %>
<li>
<h3><%= i %></h3>
<ul>
<% _.each( category, function( item ){ %>
<li>
<%= title %>
</li>
<% }) %>
</ul>
</li>
<% }) %>
</ul>
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<script>
(function() {
window.App = {
Models: {},
Collections: {},
Views: {},
Router: {}
};
window.vent = _.extend({}, Backbone.Events);
window.template = function(id) {
return _.template( $('#' + id).html() );
};
})();
// !models.js
App.Models.Item = Backbone.Model.extend({});
// !collections.js
App.Collections.Items = Backbone.Collection.extend({
model: App.Models.Item,
url: 'api/items.json'
});
// !views.js
App.Views.MyItems = Backbone.View.extend({
el: '#items',
initialize: function() {
var groups = _.groupBy(this.collection.toJSON(), 'category');
console.log(groups);
vent.on('item:edit', this.editItem, this);
var allItemsView = new App.Views.Items({ collection: App.items }).render();
$('#items').append(allItemsView.el);
},
editItem: function(item) {
var editItemView = new App.Views.EditItem({ model: item });
$('#edit-item').html(editItemView.el);
},
});
App.Views.Items = Backbone.View.extend({
tagName: 'ul',
className: 'items',
initialize: function() {
this.collection.on('add', this.addOne, this);
},
render: function() {
this.collection.each( this.addOne, this );
return this;
},
addOne: function(item) {
var itemView = new App.Views.Item({ model: item });
this.$el.append(itemView.render().el);
},
});
App.Views.Item = Backbone.View.extend({
tagName: 'li',
className: 'item',
template: template('allItemTemlate'),
initialize: function() {
this.model.on('destroy', this.unrender, this);
this.model.on('change', this.render, this);
},
render: function() {
this.$el.html( this.template( this.model.toJSON() ) );
return this;
},
});
// !router.js
App.Router = Backbone.Router.extend({
routes: {
'':'index',
},
index: function() {
console.log('index page !');
},
});
new App.Router;
Backbone.history.start();
App.items = new App.Collections.Items;
App.items.fetch().then(function() {
new App.Views.MyItems({ collection: App.items });
});
</script>
</body>
</html>
控制台中的我看到数组但是没有正确呈现.. 我不知道如何正确地完成所有功能。
答案 0 :(得分:1)
我没有读完你的所有代码,所以我不能说任何其他问题,但我可以立即看到你的groupBy函数有问题。如果使用App.Collections.Items,则不会将视图中实例化的集合传递给函数;你将把空的Collection构造函数对象传递给函数!此外,您需要将集合转换为JSON。你想要的是更像这样的东西:
var groups = _.groupBy(this.collection.toJSON(), 'category');