Create sub-collections of other collections and keep them in sync
Create sub-collections of other collections and keep them in sync.
tasks =# Create a subset of our main collectiontoday = taskssubcollection: taskisTodaytoday.url = '/tasks/today'# Create a new task in the main collection...taskscreate id: 200today: true# ...and it's automatically pushed to all subsetstodayget200 is tasksget200 # true# Model changes are pushed upwardstodayget200destroy# Create models in subsets and they are pushed to the parent collectiontodaycreate id: 201todayget201 is tasksget2001 # true# And they will update on fetchtasksfetch# When subsets are fetched, all the models are pushed to the parent collectiontodayfetch
In your web page:
Now you have two methods for creating subsets (or subcollections).
The easiest way to create a subset is to use
var TaskCollection = BackboneCollectionextend;var tasks = ;var today = taskssubcollectionreturn taskisToday;;
today will be an instance of
tasks.constructor, in this case,
If you need access to the subset object itself (you might want to modify the filter) you can create a subset directly.
var allTasks = ;var subset =parent: allTasksreturn taskisToday;;var todayTasks = subsetchild;subsetsetFilterreturn taskisYesterday;;
Depending on how you create the subset you have different interface. Creating a subcollection via
Backbone.Collection::subcollection you will only have
access to the child collection and the Backbone.CollectionSubset instance is completely transparent. However, you may need some extra flexibility so you can
Backbone.CollectionSubset instances manually
subset = parent: tasks
new_collection = taskssubcollection: modelisToday
refresh- Whether to refresh the collection from the parent immediately. Defaults to
triggers- Set which model attribute changes to watch for. This is important for performance. Defaults to
filter- The default filter. By default lets all models pass
child- The collection to use for the child. This isn't required and will be automatically created. Defaults to
parent- required The collection to base the subset on. Must be a Backbone.Collection.
If you create a Backbone.CollectionSubset instance directly you will have access to these methods.
Set the parent collection. Will unbind any events on the collection that the subset current has and setup all the events for syncing with the child collection
Same as the previous option, but with the child collection.
Set the filter used on the parent collection. This does not automatically refresh the child collection.
Refreshes the child collection with models from the parent. Clears out the child collection and refilters the parent. Triggers a 'refresh' event on the child.
Clean up the subset. Removes the child collection completely and removes all events from the parent child. Triggers a
dispose event on the subset.
subcollectionthat will prepend the parent collection url
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 "lib" subdirectory!
Copyright (c) 2012 Anthony Short Licensed under the MIT license.