Skip to content

Commit

Permalink
Add create class method to instantiate a model, add it to the collect…
Browse files Browse the repository at this point in the history
…ion and return it.
  • Loading branch information
benpickles committed Sep 10, 2010
1 parent c728c03 commit 0f81b7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/model_class_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Model.ClassMethods = {
return this.collection.length;
},

create: function(attributes) {
var model = new this(attributes)
this.add(model)
return model
},

detect: function(func) {
var all = this.all(),
model
Expand Down
10 changes: 10 additions & 0 deletions test/tests/model_class_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,13 @@ test("reverse", function() {

same(Post.reverse().all(), [post2, post1])
})

test("create", function() {
var Post = Model("post")
var post = Post.create({ title: "blah" })

equals(Post.count(), 1)
same(post, Post.first())
same(post.attributes, Post.first().attributes)
equals(post.attributes, Post.first().attributes)
})

0 comments on commit 0f81b7e

Please sign in to comment.