Skip to content
Browse files

Added exclusive for better performance

  • Loading branch information...
1 parent 7fbcd4b commit 012413b53da2f668dea25ec065893ba9cc5b4348 @masylum committed Jun 25, 2012
Showing with 49 additions and 1 deletion.
  1. +6 −0 Readme.md
  2. +34 −1 backbone.subset.js
  3. +9 −0 test/benchmarks.js
View
6 Readme.md
@@ -78,6 +78,12 @@ to be 'none' (this is the default).
* To limit which model keys trigger a live update, set `liveupdate\_keys`
to be an array of attributes: `liveupdate\_keys = ['archived']`.
+### Exclusive subsets
+
+There are subsets that are exclusive and can operate more efficiently since they don't have to
+bubble to their siblings. To mark a subset as exclusive just implement the `exclusiveSubset`
+function to return `true` or `false`.
+
## Tests
You must have node installed in order to run the tests.
View
35 backbone.subset.js
@@ -66,6 +66,15 @@
};
/**
+ * Default exclusiveSubset implementation
+ *
+ * @return {Boolean}
+ */
+ Subset.exclusiveSubset = function () {
+ return false;
+ };
+
+ /**
* Resets the parent collection
*
* @param {Object} models
@@ -96,7 +105,11 @@
xored_ids = xor(ids, _.pluck(models, 'id'));
parent.reset(parent_models, _.extend({silent: true}, options));
- parent.trigger('reset', this, _.extend({model_ids: xored_ids}, options));
+ if (this.exclusiveSubset()) {
+ parent.trigger('reset', this, _.extend({model_ids: xored_ids, exclusive_collection: this}, options));
+ } else {
+ parent.trigger('reset', this, _.extend({model_ids: xored_ids}, options));
+ }
return this;
};
@@ -157,6 +170,10 @@
* @return {Object} model
*/
Subset.add = function (model, options) {
+ if (this.exclusiveSubset()) {
+ options = _.extend(options, {exclusive_collection: this});
+ }
+
return _.result(this, 'parent').add(model, options);
};
@@ -181,6 +198,10 @@
* @return {Object} model
*/
Subset.remove = function (model, options) {
+ if (this.exclusiveSubset()) {
+ options = _.extend(options, {exclusive_collection: this});
+ }
+
return _.result(this, 'parent').remove(model, options);
};
@@ -229,6 +250,10 @@
Subset._proxyAdd = function (model, collection, options) {
options = options || {};
+ if (options.exclusive_collection && options.exclusive_collection !== this) {
+ return;
+ }
+
if (collection !== this && this.sieve(model) && !options.noproxy) {
this._addToSubset(model, options);
}
@@ -244,6 +269,10 @@
Subset._proxyRemove = function (model, collection, options) {
options = options || {};
+ if (options.exclusive_collection && options.exclusive_collection !== this) {
+ return;
+ }
+
if (collection !== this && this.sieve(model) && !options.noproxy) {
this._removeFromSubset(model, options);
}
@@ -277,6 +306,10 @@
var sieved_models
, self = this;
+ if (options.exclusive_collection && options.exclusive_collection !== this) {
+ return;
+ }
+
function getSievedModels() {
return _.filter(options.model_ids, function (id) {
var model = self.parent().get(id) || self.get(id);
View
9 test/benchmarks.js
@@ -33,6 +33,9 @@ Collections.ArchivedTasks = Backbone.Subset.extend({
parent: function () {
return tasks;
}
+, exclusiveSubset: function () {
+ return true;
+ }
, name: 'ArchivedTasks'
, sieve: function (task) {
return task.isArchived();
@@ -43,6 +46,9 @@ Collections.UrgentTasks = Backbone.Subset.extend({
parent: function () {
return tasks;
}
+, exclusiveSubset: function () {
+ return true;
+ }
, name: 'UrgentTasks'
, sieve: function (task) {
return task.isUrgent();
@@ -53,6 +59,9 @@ Collections.ProjectTasks = Backbone.Subset.extend({
beforeInitialize: function (models, options) {
this.project = options.project;
}
+, exclusiveSubset: function () {
+ return true;
+ }
, name: 'ProjectTasks'
, parent: function () {
return tasks;

0 comments on commit 012413b

Please sign in to comment.
Something went wrong with that request. Please try again.