Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Modify .extend to copy the parent's constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
maspwr committed Feb 4, 2013
1 parent ddffb2f commit ac3253a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion spec/javascripts/rosie.spec.js
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions src/rosie.js
Expand Up @@ -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];
Expand Down

0 comments on commit ac3253a

Please sign in to comment.