Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Move model collection methods into a separate object allowing you to …
Browse files Browse the repository at this point in the history
…customise the defaults for all models.
  • Loading branch information
benpickles committed Mar 21, 2010
1 parent 8e5d44b commit 0d4ef11
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 109 deletions.
1 change: 1 addition & 0 deletions src/js-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
//= require "model"
//= require "model_collection"
//= require "model_collection_methods"
//= require "model_errors"
//= require "model_instance_methods"
//= require "model_log"
Expand Down
113 changes: 4 additions & 109 deletions src/model_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,13 @@ Model.Collection = function() {
this.callbacks = {};
};

model_collection.prototype = {
add: function() {
var added = [];

for (var i = 0; i < arguments.length; i++) {
var model = arguments[i];
var existing_elem = this.detect(function() {
return this.id() != null && this.id() == model.id();
});

if (!existing_elem) {
this.collection.push(model);
added.push(model);
}
};

if (added.length > 0) this.trigger("add", added);

return this;
},

all: function() {
return this.collection;
},

bind: function(event, callback) {
this.callbacks[event] = this.callbacks[event] || [];
this.callbacks[event].push(callback);
return this;
},

// Convenience method to allow a simple way to chain collection methods.
jQuery.extend(model_collection.prototype, Model.CollectionMethods, {
// Convenience method to allow a simple method of chaining collection
// methods.
chain: function(collection) {
return new model_collection(collection);
},

count: function() {
return this.collection.length;
},

detect: function(func) {
var model;
$.each(this.all(), function(i) {
if (func.call(this, i)) {
model = this;
return false;
}
});
return model;
},

each: function(func) {
jQuery.each(this.all(), function(i) {
func.call(this, i);
});
return this;
},

find: function(id) {
return this.detect(function() {
return this.id() == id;
});
},

first: function() {
return this.all()[0] || null;
},

last: function() {
var all = this.all();
return all[all.length - 1] || null;
},

remove: function(id) {
var ids = _.invoke(this.collection, 'id');
var index = _.indexOf(ids, id);
if (index > -1) {
this.collection.splice(index, 1);
this.trigger("remove");
return true;
} else {
return false;
};
},

select: function(func) {
var selected = [];
$.each(this.all(), function(i) {
if (func.call(this, i)) selected.push(this);
});
return this.chain(selected);
},

sort: function(func) {
var sorted = _.sortBy(this.all(), function(model, i) {
return func.call(model, i);
});
return this.chain(sorted);
},

trigger: function(name, data) {
var callbacks = this.callbacks[name];

if (callbacks) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].apply(this, data || []);
};
};

return this;
}
};
});

return new model_collection();
};
107 changes: 107 additions & 0 deletions src/model_collection_methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Model.CollectionMethods = {
add: function() {
var added = [];

for (var i = 0; i < arguments.length; i++) {
var model = arguments[i];
var existing_elem = this.detect(function() {
return this.id() != null && this.id() == model.id();
});

if (!existing_elem) {
this.collection.push(model);
added.push(model);
}
};

if (added.length > 0) this.trigger("add", added);

return this;
},

all: function() {
return this.collection;
},

bind: function(event, callback) {
this.callbacks[event] = this.callbacks[event] || [];
this.callbacks[event].push(callback);
return this;
},

count: function() {
return this.collection.length;
},

detect: function(func) {
var model;
$.each(this.all(), function(i) {
if (func.call(this, i)) {
model = this;
return false;
}
});
return model;
},

each: function(func) {
jQuery.each(this.all(), function(i) {
func.call(this, i);
});
return this;
},

find: function(id) {
return this.detect(function() {
return this.id() == id;
});
},

first: function() {
return this.all()[0] || null;
},

last: function() {
var all = this.all();
return all[all.length - 1] || null;
},

remove: function(id) {
var ids = _.invoke(this.collection, 'id');
var index = _.indexOf(ids, id);
if (index > -1) {
this.collection.splice(index, 1);
this.trigger("remove");
return true;
} else {
return false;
};
},

select: function(func) {
var selected = [];
$.each(this.all(), function(i) {
if (func.call(this, i)) selected.push(this);
});
return this.chain(selected);
},

sort: function(func) {
var sorted = _.sortBy(this.all(), function(model, i) {
return func.call(model, i);
});
return this.chain(sorted);
},

trigger: function(name, data) {
var callbacks = this.callbacks[name];

if (callbacks) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].apply(this, data || []);
};
};

return this;
}
};
1 change: 1 addition & 0 deletions test/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- js-model -->
<script src="src/model.js"></script>
<script src="src/model_collection.js"></script>
<script src="src/model_collection_methods.js"></script>
<script src="src/model_errors.js"></script>
<script src="src/model_instance_methods.js"></script>
<script src="src/model_log.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/public/src/model_collection_methods.js

0 comments on commit 0d4ef11

Please sign in to comment.