Skip to content

Commit

Permalink
Prototypal inheritance.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Apr 4, 2011
1 parent 01f2286 commit 7523d56
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def files
model_rest
model_uid
model_version
model_base
)
end

Expand Down
5 changes: 3 additions & 2 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ var Model = function(name, func) {
model
.extend(Model.Callbacks)
.extend(Model.ClassMethods)
.include(Model.Callbacks)
.include(Model.InstanceMethods)

model.prototype = new Model.Base
model.prototype.constructor = model

if (jQuery.isFunction(func)) func.call(model, model, model.prototype)

Expand Down
5 changes: 5 additions & 0 deletions src/model_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Model.Base = (function() {
function Base() {}
Base.prototype = jQuery.extend({}, Model.Callbacks, Model.InstanceMethods)
return Base
})();
18 changes: 18 additions & 0 deletions test/tests/inheritance_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module("Model inheritance")

test("methods added to Model.Base after an instance has been defined", function() {
var Post = Model("post")
var post = new Post({ title: "upper" })

ok(post.attrUpper === undefined)

Model.Base.prototype.attrUpper = function(attr) {
return this.attr(attr).toUpperCase()
}

equal("UPPER", post.attrUpper("title"))

delete Model.Base.prototype.attrUpper

ok(post.attrUpper === undefined)
})
1 change: 1 addition & 0 deletions test/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<script src="tests/model_rest_test.js"></script>
<script src="tests/model_uid_test.js"></script>
<script src="tests/model_local_storage_test.js"></script>
<script src="tests/inheritance_test.js"></script>
</head>
<body>
<h1 id="qunit-header">js-model Tests</h1>
Expand Down

0 comments on commit 7523d56

Please sign in to comment.