Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
backbone.collectionview
Advanced tools
A view optimized for rendering collections. Give the CollectionView a collection and a view to render each item with and it will take care of updating the view with the collection.
Download the production version or the development version.
In your web page:
<script src="dist/backbone.collectionview.min.js"></script>
Create a CollectionView and pass it the collection and the item view.
// Our collection of tasks
var tasks = new Backbone.Collection();
// Create a view for a task list item
var TaskListItemView = Backbone.View.extend({
template: 'templates/task'
});
// Create a collection view. Tell it which view to
// use for rendering the items. Alternatively this can be passed
// into the options when creating a collectionview instance.
var TaskListView = Backbone.CollectionView.extend({
itemView: taskListItemView
});
// Now create an instance of the CollectionView
var todolist = new TaskListView({
collection: tasks
});
// Render it on the page
todolist.render();
todolist.renderAllItems();
// Add an item to the list and the collection
// Listens for the 'add' event and creates a view
tasks.create({
title: 'Pick up milk'
});
// Shortcut to directly create a model in the collection
// via the collection view.
todolist.add({
title: 'Eat some bacon'
});
The first thing you need to know about creating a collection view is that it needs a view for the items and a collection.
The item view is the view that is used to render each of the models in the collection. eg. In a list of tasks the item view is the view for a single task item. Item views will have access to the model and the collection by default.
You can set the item view in a few ways:
var TaskListView = Backbone.CollectionView.extend({
itemView: myItemView
});
var TaskListView = Backbone.CollectionView.extend();
var list = new TaskListView({
itemView: myItemView
});
You can use this to override the view set in the original definition as well. This allows you to use the same collection view for different types of lists that have the same functionality. For example, a list of albums in a library may have a cover view and a title view.
If you want to do some processing on the view before it's used then you can override the getItemView
method.
var TaskListView = Backbone.CollectionView.extend({
getItemView: function(model){
return new myItemView({
model: model
});
}
});
render
- Automatically render when an instance is createdrenderItems
- Automatically render the items. This will also call render
itemView
- The view to use to render each item in the collectioncollection
- required The collection whose models will be rendered.There is a method named afterRenderItem
that will be fired whenever an item is rendered in the list.
afterRenderItem: function(view, model) {}
You can override this method if you need to do some extra processing on views after they are rendered.
You can easily listen for events on the views in the collection too. Just add a viewEvents
object to your collection view.
This is handy for things like selections.
var TaskListItem = Backbone.View.extend({
events: {
'click': 'select'
},
select: function(event) {
this.trigger('select', this);
}
});
var TaskListView = Backbone.CollectionView.extend({
itemView: TaskListItem,
viewEvents: {
'select': 'onItemSelect'
},
initialize: function(options) {
this.selected = [];
},
onItemSelect: function(view) {
this.selected.push(view);
}
});
You can also listen for events on the collection easilt as well.
var TaskListView = Backbone.CollectionView.extend({
itemView: TaskListItem,
collectionEvents: {
'sort': 'onCollectionSort'
},
onCollectionSort: function(view) {
# Do sorting stuff
}
});
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "src" subdirectory!
0.1.0 - First release
Copyright (c) 2012 Anthony Short
Licensed under the MIT license.
FAQs
A view optimized for rendering collections
The npm package backbone.collectionview receives a total of 1 weekly downloads. As such, backbone.collectionview popularity was classified as not popular.
We found that backbone.collectionview demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.