Categories
backbone.js

Backbone.js: binding collection resets

this.collection.bind(‘reset’, this.render, this);

This is run in a view, probably in the initialization function. When the collection gets data from backend, it resets. The last “this” indicates context (in this case, it means the render will happen in this view).

Looping through collections:

_(this.collection.models).each(function (result) {
currentNode.append(new CatalogNodeTableView({model: result}).render().el);
});

 

Templates: example of an each iteration

<% _.each(catalogNodes.root.children, function(node) { %>

<% var nodeType; if(!_.isUndefined(node.type)){nodeType = ‘catalog’;} else if(!_.isUndefined(node.childCategoryIds)){nodeType = ‘folder’;} else {nodeType = ‘item’;} %>

<div class=’grid2col last catalogNode’ data-node-id='<%= node.id %>’ data-node-type='<%= nodeType %>’><%= node.name %></div>

<% }); %>