Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow setting parent for each instance of the subset collection. fixes #4 #5

Closed
wants to merge 1 commit into from

3 participants

@saimonmoore
Collaborator

e.g. new Collections.UrgentTasks([], {parent: tasks});

This will override the #parent method defined on the class (if any)
or fallback to it if no parent option is supplied to the constructor.

Prompted by discussion here: #4

@saimonmoore saimonmoore Allow setting parent for each instance of the subset collection.
e.g.  new Collections.UrgentTasks([], {parent: tasks});

This will override the #parent method defined on the class (if any)
or fallback to it if no parent option is supplied to the constructor.
d1a4287
@malandrew

Any chance this commit will be pulled into master?

@saimonmoore
Collaborator

I want to rework this next. week and then merge it in

@masylum
Owner

This is already on the master

@masylum masylum closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 24, 2012
  1. @saimonmoore

    Allow setting parent for each instance of the subset collection.

    saimonmoore authored
    e.g.  new Collections.UrgentTasks([], {parent: tasks});
    
    This will override the #parent method defined on the class (if any)
    or fallback to it if no parent option is supplied to the constructor.
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 16 deletions.
  1. +12 −11 backbone.subset.js
  2. +2 −5 test/test.js
View
23 backbone.subset.js
@@ -23,13 +23,14 @@
Backbone.Subset = function Subset(models, options) {
options = options || {};
- this.model = this.parent().model;
- this.comparator = this.comparator || options.comparator || this.parent().comparator;
+ this._parent = options.parent || (this.parent && this.parent());
+ this.model = this._parent.model;
+ this.comparator = this.comparator || options.comparator || this._parent.comparator;
this.liveupdate_keys = this.liveupdate_keys || options.liveupdate_keys || 'none';
_.bindAll(this, '_onModelEvent', '_unbindModelEvents', '_proxyEvents');
- this.parent().bind('all', this._proxyEvents);
+ this._parent.bind('all', this._proxyEvents);
if (this.beforeInitialize) {
this.beforeInitialize.apply(this, arguments);
@@ -43,7 +44,7 @@
}
}
else {
- this._resetSubset(this.parent().models, {silent: true});
+ this._resetSubset(this._parent.models, {silent: true});
}
this.initialize.apply(this, arguments);
@@ -57,7 +58,7 @@
* @return {Object} collection
*/
Subset.reset = function (models, options) {
- var parent_models = _.clone(this.parent().models)
+ var parent_models = _.clone(this._parent.models)
, ids = _(parent_models).pluck('id');
models = models || [];
@@ -71,7 +72,7 @@
}
}, this);
- this.parent().reset(parent_models, _.extend(options, {subset_reset: true}));
+ this._parent.reset(parent_models, _.extend(options, {subset_reset: true}));
this._resetSubset(models, options);
@@ -111,7 +112,7 @@
* @return {Object} model
*/
Subset._add = function (model, options) {
- return this.parent()._add(model, options);
+ return this._parent._add(model, options);
};
/**
@@ -124,7 +125,7 @@
Subset._addToSubset = function (model, options) {
var parents_model;
- if (model.id && (parents_model = this.parent().get(model.id))) {
+ if (model.id && (parents_model = this._parent.get(model.id))) {
if (!(model instanceof Backbone.Model)) {
parents_model.set(model, {silent: true});
model = parents_model;
@@ -151,7 +152,7 @@
* @return {Object} model
*/
Subset._remove = function (model, options) {
- return this.parent()._remove(model, options);
+ return this._parent._remove(model, options);
};
/**
@@ -175,13 +176,13 @@
Subset._prepareModel = function (model, options) {
if (!(model instanceof Backbone.Model)) {
var attrs = model;
- model = new this.model(attrs, {collection: this.parent()});
+ model = new this.model(attrs, {collection: this._parent});
if (model.validate && !model._performValidation(model.attributes, options)) {
model = false;
}
} else if (!model.collection) {
- model.collection = this.parent();
+ model.collection = this._parent;
}
model = this.sieve(model) ? model : false;
return model;
View
7 test/test.js
@@ -51,10 +51,7 @@ Collections.ArchivedTasks = Backbone.Subset.extend({
});
Collections.UrgentTasks = Backbone.Subset.extend({
- parent: function () {
- return tasks;
- }
-, sieve: function (task) {
+ sieve: function (task) {
return task.isUrgent();
}
, comparator: function (m) {
@@ -85,7 +82,7 @@ Collections.ProjectTasks = Backbone.Subset.extend({
tasks = new Collections.Tasks();
archived_tasks = new Collections.ArchivedTasks();
-urgent_tasks = new Collections.UrgentTasks();
+urgent_tasks = new Collections.UrgentTasks([], {parent: tasks});
// lengths
Something went wrong with that request. Please try again.