Skip to content

Commit

Permalink
Switch to using the a Collection object.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Feb 21, 2012
1 parent 5e8f4d1 commit b09e2df
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 492 deletions.
2 changes: 1 addition & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var Model = function(name, func) {
Model.Module.extend.call(model, Model.Module)

model._name = name
model.collection = []
model.collection = new Model.Collection()
model.persistence = Model.NullPersistence
model.unique_key = "id"
model
Expand Down
142 changes: 3 additions & 139 deletions src/model_class_methods.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,16 @@
Model.ClassMethods = {
add: function(model) {
var id = model.id()

if (Model.Utils.inArray(this.collection, model) === -1 && !(id && this.find(id))) {
this.collection.push(model)
this.trigger("add", [model])
}

return this;
},

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

// Convenience method to allow a simple method of chaining class methods.
chain: function(collection) {
return Model.Utils.extend({}, this, { collection: collection })
},

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

detect: function(func) {
var all = this.all(),
model

for (var i = 0, length = all.length; i < length; i++) {
model = all[i]
if (func.call(model, model, i)) return model
}
},

each: function(func, context) {
var all = this.all()

for (var i = 0, length = all.length; i < length; i++) {
func.call(context || all[i], all[i], i, all)
}

return this;
},

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

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

load: function(callback) {
var self = this

this.persistence.read(function(models) {
for (var i = 0, length = models.length; i < length; i++) {
self.add(models[i])
self.collection.add(models[i])
}

if (callback) callback.call(self, models)
Expand All @@ -67,94 +19,6 @@ Model.ClassMethods = {
return this
},

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

map: function(func, context) {
var all = this.all()
var values = []

for (var i = 0, length = all.length; i < length; i++) {
values.push(func.call(context || all[i], all[i], i, all))
}

return values
},

pluck: function(attribute) {
var all = this.all()
var plucked = []

for (var i = 0, length = all.length; i < length; i++) {
plucked.push(all[i].attr(attribute))
}

return plucked
},

remove: function(model) {
var index

for (var i = 0, length = this.collection.length; i < length; i++) {
if (this.collection[i] === model) {
index = i
break
}
}

if (index != undefined) {
this.collection.splice(index, 1);
this.trigger("remove", [model]);
return true;
} else {
return false;
}
},

reverse: function() {
return this.chain(this.all().reverse())
},

select: function(func, context) {
var all = this.all(),
selected = [],
model

for (var i = 0, length = all.length; i < length; i++) {
model = all[i]
if (func.call(context || model, model, i, all)) selected.push(model)
}

return this.chain(selected);
},

sort: function(func) {
var sorted = this.all().sort(func)
return this.chain(sorted);
},

sortBy: function(attribute_or_func) {
var is_func = Model.Utils.isFunction(attribute_or_func)
var extract = function(model) {
return attribute_or_func.call(model)
}

return this.sort(function(a, b) {
var a_attr = is_func ? extract(a) : a.attr(attribute_or_func)
var b_attr = is_func ? extract(b) : b.attr(attribute_or_func)

if (a_attr < b_attr) {
return -1
} else if (a_attr > b_attr) {
return 1
} else {
return 0
}
})
},

use: function(plugin) {
var args = Array.prototype.slice.call(arguments, 1)
args.unshift(this)
Expand Down
4 changes: 3 additions & 1 deletion src/model_local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
read: function(callback) {
if (!callback) return

var existing_uids = this.klass.map(function(model) { return model.uid })
var existing_uids = this.klass.collection.map(function(model) {
return model.uid
})
var uids = get(this.collection_id) || []
var models = []
var attributes, model, uid
Expand Down
4 changes: 2 additions & 2 deletions src/model_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Model.Model.prototype = {

this.constructor.persistence.destroy(this, function(success) {
if (success) {
self.constructor.remove(self)
self.constructor.collection.remove(self)
self.trigger("destroy")
}

Expand Down Expand Up @@ -65,7 +65,7 @@ Model.Model.prototype = {
if (success) {
Model.Utils.extend(self.attributes, self.changes)
self.reset()
self.constructor.add(self)
self.constructor.collection.add(self)
self.trigger("save")
}

Expand Down
13 changes: 6 additions & 7 deletions test/tests/model_callbacks_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ test("class-level", function() {
results.push("not-called");
});

Post
.add(post1)
.add(post2)
.add(post1)
.add(post3)
Post.remove(post1);
Post.remove(666);
Post.collection.add(post1)
Post.collection.add(post2)
Post.collection.add(post1)
Post.collection.add(post3)
Post.collection.remove(post1);
Post.collection.remove(666);
Post.trigger("custom",[post1]);

deepEqual(results, [
Expand Down
Loading

0 comments on commit b09e2df

Please sign in to comment.