Skip to content

Commit

Permalink
Add Model.extend to add class methods to the model.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Feb 10, 2011
1 parent 99a576c commit 7fb24b8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def files
model_instance_methods
model_local_storage
model_log
model_module
model_rest
model_uid
model_version
Expand Down
15 changes: 9 additions & 6 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ var Model = function(name, func) {
if (jQuery.isFunction(this.initialize)) this.initialize()
};

// Apply class methods and extend with any custom class methods. Make sure
// vitals are added last so they can't be overridden.
jQuery.extend(model, Model.Callbacks, Model.ClassMethods, {
_name: name,
collection: []
});
// Use module functionality to extend itself onto the constructor. Meta!
Model.Module.extend.call(model, Model.Module)

model._name = name
model.collection = []
model.unique_key = "id"
model
.extend(Model.Callbacks)
.extend(Model.ClassMethods)

// Add default and custom instance methods.
jQuery.extend(model.prototype, Model.Callbacks, Model.InstanceMethods)
Expand Down
2 changes: 0 additions & 2 deletions src/model_class_methods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Model.ClassMethods = {
unique_key: 'id',

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

Expand Down
6 changes: 6 additions & 0 deletions src/model_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Model.Module = {
extend: function(obj) {
jQuery.extend(this, obj)
return this
}
};
14 changes: 14 additions & 0 deletions test/tests/model_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module("Model.Module")

test("extend", function() {
var A = Model("A")
var B = Model("B")
var module = {
greet: function() { return "Hi " + this._name }
}
A.extend(module)
B.extend(module)

equals(A.greet(), "Hi A")
equals(B.greet(), "Hi B")
})
1 change: 1 addition & 0 deletions test/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="tests/model_callbacks.js"></script>
<script src="tests/model_class_methods.js"></script>
<script src="tests/model_errors.js"></script>
<script src="tests/model_module.js"></script>
<script src="tests/model_rest.js"></script>
<script src="tests/model_uid.js"></script>
<script src="tests/model_local_storage.js"></script>
Expand Down

0 comments on commit 7fb24b8

Please sign in to comment.