Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Potential fix for #3051. Add isModel method to collections. #3052

Merged
merged 2 commits into from

6 participants

@HenrikJoreteg

One possible solution that seems really simple and configurable to me.

@jashkenas jashkenas added the change label
@asakusuma

+1. This would be very useful.

@derekbrown

This would be absolutely HUGE for the application that my team and I are building.

@wookiehangover
Collaborator

+1 I have a few apps that would benefit from this

@jashkenas
Owner

Alright — let's do it. Henrik ... mind renaming the internal method to _isModel, and rebasing for a clean merge?

@HenrikJoreteg
backbone.js
@@ -649,6 +649,12 @@
// initialization logic.
initialize: function(){},
+ // Method for checking whether an object should be considered a model for
+ // the purposes of adding to the collection.
+ isModel: function (model) {
+ return model instanceof Model
@caseywebdev Collaborator

Might want to throw a ; at the end here to match the project's style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@HenrikJoreteg

How's that? I think it's good to go.

@jashkenas jashkenas merged commit 3caba2a into jashkenas:master
@jashkenas jashkenas added the fixed label
@caseywebdev
Collaborator

Coolio, nice addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 2 deletions.
  1. +8 −2 backbone.js
View
10 backbone.js
@@ -707,7 +707,7 @@
// from being added.
for (var i = 0, length = models.length; i < length; i++) {
attrs = models[i] || {};
- if (attrs instanceof Model) {
+ if (this._isModel(attrs)) {
id = model = attrs;
} else if (targetProto.generateId) {
id = targetProto.generateId(attrs);
@@ -939,7 +939,7 @@
// Prepare a hash of attributes (or other model) to be added to this
// collection.
_prepareModel: function(attrs, options) {
- if (attrs instanceof Model) {
+ if (this._isModel(attrs)) {
if (!attrs.collection) attrs.collection = this;
return attrs;
}
@@ -951,6 +951,12 @@
return false;
},
+ // Method for checking whether an object should be considered a model for
+ // the purposes of adding to the collection.
+ _isModel: function (model) {
+ return model instanceof Model;
+ },
+
// Internal method to create a model's ties to a collection.
_addReference: function(model, options) {
this._byId[model.cid] = model;
Something went wrong with that request. Please try again.