Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modify .extend to copy the parent's constructor

  • Loading branch information...
commit ac3253ac11850221cf0053b8bbd0526198bf2d31 1 parent ddffb2f
@maspwr maspwr authored
Showing with 20 additions and 1 deletion.
  1. +18 −1 spec/javascripts/rosie.spec.js
  2. +2 −0  src/rosie.js
View
19 spec/javascripts/rosie.spec.js
@@ -47,11 +47,28 @@ describe('Factory', function() {
});
describe('extend', function() {
+ var Thing = function(attrs) {
+ for(var attr in attrs) {
+ this[attr] = attrs[attr];
+ }
+ };
+ var Thingy = function(attrs) {
+ for(var attr in attrs) {
+ this[attr] = attrs[attr];
+ }
+ };
+
beforeEach(function() {
- Factory.define('thing').attr('name', 'Thing 1').after(function(obj) {
+ Factory.define('thing', Thing).attr('name', 'Thing 1').after(function(obj) {
obj.afterCalled = true;
});
Factory.define('anotherThing').extend('thing').attr('title', 'Title 1');
+ Factory.define('differentThing', Thingy).extend('thing').attr('title', 'Title 1');
+ });
+
+ it('should extend the constructor', function() {
+ expect(Factory.build('anotherThing') instanceof Thing).toBe(true);
+ expect(Factory.build('differentThing') instanceof Thingy).toBe(true);
});
it('should extend attributes', function() {
View
2  src/rosie.js
@@ -44,6 +44,8 @@ Factory.prototype = {
extend: function(name) {
var factory = Factory.factories[name];
+ // Copy the parent's constructor
+ if (this.construct === undefined) { this.construct = factory.construct; }
for(var attr in factory.attrs) {
if(factory.attrs.hasOwnProperty(attr)) {
this.attrs[attr] = factory.attrs[attr];
Please sign in to comment.
Something went wrong with that request. Please try again.