This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
forked from benpickles/js-model
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move model collection methods into a separate object allowing you to …
…customise the defaults for all models.
- Loading branch information
1 parent
8e5d44b
commit 0d4ef11
Showing
5 changed files
with
114 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../src/model_collection_methods.js |