Skip to content

Commit

Permalink
Allow services created with $extend to be called without the new keyw…
Browse files Browse the repository at this point in the history
…ord.
  • Loading branch information
katowulf committed Apr 17, 2015
1 parent 91414e0 commit 504383d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
8 changes: 7 additions & 1 deletion src/FirebaseArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,13 @@
FirebaseArray.$extend = function(ChildClass, methods) {
if( arguments.length === 1 && angular.isObject(ChildClass) ) {
methods = ChildClass;
ChildClass = function() { return FirebaseArray.apply(this, arguments); };
ChildClass = function(ref) {
if( !(this instanceof ChildClass) ) {
return new ChildClass(ref);
}
FirebaseArray.apply(this, arguments);
return this.$list;
};
}
return $firebaseUtils.inherit(ChildClass, FirebaseArray, methods);
};
Expand Down
7 changes: 6 additions & 1 deletion src/FirebaseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@
FirebaseObject.$extend = function(ChildClass, methods) {
if( arguments.length === 1 && angular.isObject(ChildClass) ) {
methods = ChildClass;
ChildClass = function() { FirebaseObject.apply(this, arguments); };
ChildClass = function(ref) {
if( !(this instanceof ChildClass) ) {
return new ChildClass(ref);
}
FirebaseObject.apply(this, arguments);
};
}
return $firebaseUtils.inherit(ChildClass, FirebaseObject, methods);
};
Expand Down
25 changes: 0 additions & 25 deletions tests/mocks/mock.utils.js

This file was deleted.

16 changes: 14 additions & 2 deletions tests/unit/FirebaseArray.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ describe('$firebaseArray', function () {
describe('$extend', function() {
it('should return a valid array', function() {
var F = $firebaseArray.$extend({});
expect(Array.isArray(new F(stubRef()))).toBe(true);
expect(Array.isArray(F(stubRef()))).toBe(true);
});

it('should preserve child prototype', function() {
Expand All @@ -809,11 +809,23 @@ describe('$firebaseArray', function () {
it('should add on methods passed into function', function() {
function foo() { return 'foo'; }
var F = $firebaseArray.$extend({foo: foo});
var res = new F(stubRef());
var res = F(stubRef());
expect(typeof res.$$updated).toBe('function');
expect(typeof res.foo).toBe('function');
expect(res.foo()).toBe('foo');
});

it('should work with the new keyword', function() {
var fn = function() {};
var Res = $firebaseArray.$extend({foo: fn});
expect(new Res(stubRef()).foo).toBeA('function');
});

it('should work without the new keyword', function() {
var fn = function() {};
var Res = $firebaseArray.$extend({foo: fn});
expect(Res(stubRef()).foo).toBeA('function');
});
});

var flushAll = (function() {
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/FirebaseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,24 @@ describe('$firebaseObject', function() {
return 'foo';
}
var F = $firebaseObject.$extend({foo: foo});
var res = new F(stubRef());
var res = F(stubRef());
expect(res.$$updated).toBeA('function');
expect(res.foo).toBeA('function');
expect(res.foo()).toBe('foo');
});


it('should work with the new keyword', function() {
var fn = function() {};
var Res = $firebaseObject.$extend({foo: fn});
expect(new Res(stubRef()).foo).toBeA('function');
});

it('should work without the new keyword', function() {
var fn = function() {};
var Res = $firebaseObject.$extend({foo: fn});
expect(Res(stubRef()).foo).toBeA('function');
});
});

describe('$$updated', function () {
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/firebase.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
'use strict';
describe('$firebase', function () {

var $firebase, $timeout, $rootScope, $utils;

var defaults = JSON.parse(window.__html__['fixtures/data.json']);

beforeEach(function () {
module('firebase');
module('mock.utils');
});

describe('<constructor>', function () {
Expand Down

0 comments on commit 504383d

Please sign in to comment.