Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 504383d

Browse files
committed
Allow services created with $extend to be called without the new keyword.
1 parent 91414e0 commit 504383d

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

src/FirebaseArray.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,13 @@
576576
FirebaseArray.$extend = function(ChildClass, methods) {
577577
if( arguments.length === 1 && angular.isObject(ChildClass) ) {
578578
methods = ChildClass;
579-
ChildClass = function() { return FirebaseArray.apply(this, arguments); };
579+
ChildClass = function(ref) {
580+
if( !(this instanceof ChildClass) ) {
581+
return new ChildClass(ref);
582+
}
583+
FirebaseArray.apply(this, arguments);
584+
return this.$list;
585+
};
580586
}
581587
return $firebaseUtils.inherit(ChildClass, FirebaseArray, methods);
582588
};

src/FirebaseObject.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,12 @@
287287
FirebaseObject.$extend = function(ChildClass, methods) {
288288
if( arguments.length === 1 && angular.isObject(ChildClass) ) {
289289
methods = ChildClass;
290-
ChildClass = function() { FirebaseObject.apply(this, arguments); };
290+
ChildClass = function(ref) {
291+
if( !(this instanceof ChildClass) ) {
292+
return new ChildClass(ref);
293+
}
294+
FirebaseObject.apply(this, arguments);
295+
};
291296
}
292297
return $firebaseUtils.inherit(ChildClass, FirebaseObject, methods);
293298
};

tests/mocks/mock.utils.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/unit/FirebaseArray.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ describe('$firebaseArray', function () {
783783
describe('$extend', function() {
784784
it('should return a valid array', function() {
785785
var F = $firebaseArray.$extend({});
786-
expect(Array.isArray(new F(stubRef()))).toBe(true);
786+
expect(Array.isArray(F(stubRef()))).toBe(true);
787787
});
788788

789789
it('should preserve child prototype', function() {
@@ -809,11 +809,23 @@ describe('$firebaseArray', function () {
809809
it('should add on methods passed into function', function() {
810810
function foo() { return 'foo'; }
811811
var F = $firebaseArray.$extend({foo: foo});
812-
var res = new F(stubRef());
812+
var res = F(stubRef());
813813
expect(typeof res.$$updated).toBe('function');
814814
expect(typeof res.foo).toBe('function');
815815
expect(res.foo()).toBe('foo');
816816
});
817+
818+
it('should work with the new keyword', function() {
819+
var fn = function() {};
820+
var Res = $firebaseArray.$extend({foo: fn});
821+
expect(new Res(stubRef()).foo).toBeA('function');
822+
});
823+
824+
it('should work without the new keyword', function() {
825+
var fn = function() {};
826+
var Res = $firebaseArray.$extend({foo: fn});
827+
expect(Res(stubRef()).foo).toBeA('function');
828+
});
817829
});
818830

819831
var flushAll = (function() {

tests/unit/FirebaseObject.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,24 @@ describe('$firebaseObject', function() {
587587
return 'foo';
588588
}
589589
var F = $firebaseObject.$extend({foo: foo});
590-
var res = new F(stubRef());
590+
var res = F(stubRef());
591591
expect(res.$$updated).toBeA('function');
592592
expect(res.foo).toBeA('function');
593593
expect(res.foo()).toBe('foo');
594594
});
595+
596+
597+
it('should work with the new keyword', function() {
598+
var fn = function() {};
599+
var Res = $firebaseObject.$extend({foo: fn});
600+
expect(new Res(stubRef()).foo).toBeA('function');
601+
});
602+
603+
it('should work without the new keyword', function() {
604+
var fn = function() {};
605+
var Res = $firebaseObject.$extend({foo: fn});
606+
expect(Res(stubRef()).foo).toBeA('function');
607+
});
595608
});
596609

597610
describe('$$updated', function () {

tests/unit/firebase.spec.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
'use strict';
22
describe('$firebase', function () {
33

4-
var $firebase, $timeout, $rootScope, $utils;
5-
6-
var defaults = JSON.parse(window.__html__['fixtures/data.json']);
7-
84
beforeEach(function () {
95
module('firebase');
10-
module('mock.utils');
116
});
127

138
describe('<constructor>', function () {

0 commit comments

Comments
 (0)