Fixing a small bug in model inheritance: Class properties need to be inherited (along with the instance properties). See test.

This commit is contained in:
Samuel Clay
2011-01-05 16:35:05 -05:00
parent c933262555
commit 440d1415fb
2 changed files with 27 additions and 0 deletions

View File

@@ -1004,6 +1004,9 @@
child = function(){ return parent.apply(this, arguments); };
}
// Inherit class (static) properties from parent.
_.extend(child, parent);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
ctor.prototype = parent.prototype;

View File

@@ -320,5 +320,29 @@ $(document).ready(function() {
equals(lastError, "Can't change admin status.");
equals(boundError, undefined);
});
test("Model: Inherit class properties", function() {
var Parent = Backbone.Model.extend({
instancePropSame: function() {},
instancePropDiff: function() {}
}, {
classProp: function() {}
});
var Child = Parent.extend({
instancePropDiff: function() {}
});
var adult = new Parent;
var kid = new Child;
equals(Child.classProp, Parent.classProp);
notEqual(Child.classProp, undefined);
equals(kid.instancePropSame, adult.instancePropSame);
notEqual(kid.instancePropSame, undefined);
notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
notEqual(Child.prototype.instancePropDiff, undefined);
});
});