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

Commit b37d659

Browse files
committed
Fixes #385 - not accepting factory names for arrayFactory and objectFactory
1 parent e0e613a commit b37d659

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

dist/angularfire.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,13 +1512,23 @@ if ( typeof Object.getPrototypeOf !== "function" ) {
15121512
'use strict';
15131513

15141514
angular.module('firebase')
1515-
.factory('$firebaseConfig', ["$FirebaseArray", "$FirebaseObject",
1516-
function($FirebaseArray, $FirebaseObject) {
1515+
.factory('$firebaseConfig', ["$FirebaseArray", "$FirebaseObject", "$injector",
1516+
function($FirebaseArray, $FirebaseObject, $injector) {
15171517
return function(configOpts) {
1518+
// make a copy we can modify
1519+
var opts = angular.extend({}, configOpts);
1520+
// look up factories if passed as string names
1521+
if( typeof opts.objectFactory === 'string' ) {
1522+
opts.objectFactory = $injector.get(opts.objectFactory);
1523+
}
1524+
if( typeof opts.arrayFactory === 'string' ) {
1525+
opts.arrayFactory = $injector.get(opts.arrayFactory);
1526+
}
1527+
// extend defaults and return
15181528
return angular.extend({
15191529
arrayFactory: $FirebaseArray,
15201530
objectFactory: $FirebaseObject
1521-
}, configOpts);
1531+
}, opts);
15221532
};
15231533
}
15241534
])

dist/angularfire.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FirebaseArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
.then(function(ref) {
111111
self._notify('child_changed', key);
112112
return ref;
113-
})
113+
});
114114
}
115115
else {
116116
return $firebaseUtils.reject('Invalid record; could determine its key: '+indexOrItem);

src/utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22
'use strict';
33

44
angular.module('firebase')
5-
.factory('$firebaseConfig', ["$FirebaseArray", "$FirebaseObject",
6-
function($FirebaseArray, $FirebaseObject) {
5+
.factory('$firebaseConfig', ["$FirebaseArray", "$FirebaseObject", "$injector",
6+
function($FirebaseArray, $FirebaseObject, $injector) {
77
return function(configOpts) {
8+
// make a copy we can modify
9+
var opts = angular.extend({}, configOpts);
10+
// look up factories if passed as string names
11+
if( typeof opts.objectFactory === 'string' ) {
12+
opts.objectFactory = $injector.get(opts.objectFactory);
13+
}
14+
if( typeof opts.arrayFactory === 'string' ) {
15+
opts.arrayFactory = $injector.get(opts.arrayFactory);
16+
}
17+
// extend defaults and return
818
return angular.extend({
919
arrayFactory: $FirebaseArray,
1020
objectFactory: $FirebaseObject
11-
}, configOpts);
21+
}, opts);
1222
};
1323
}
1424
])

tests/unit/firebase.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ describe('$firebase', function () {
77
module('firebase');
88
module('mock.firebase');
99
module('mock.utils');
10+
// have to create these before the first call to inject
11+
// or they will not be registered with the angular mock injector
12+
angular.module('firebase').provider('TestArrayFactory', {
13+
$get: function() {
14+
return function() {}
15+
}
16+
}).provider('TestObjectFactory', {
17+
$get: function() {
18+
return function() {};
19+
}
20+
});
1021
inject(function (_$firebase_, _$timeout_, _$rootScope_, $firebaseUtils) {
1122
$firebase = _$firebase_;
1223
$timeout = _$timeout_;
@@ -32,6 +43,39 @@ describe('$firebase', function () {
3243
$firebase('hello world');
3344
}).toThrowError(/valid Firebase reference/);
3445
});
46+
47+
it('should accept a factory name for arrayFactory', function() {
48+
var ref = new Firebase('Mock://');
49+
var app = angular.module('firebase');
50+
// if this does not throw an error we are fine
51+
expect($firebase(ref, {arrayFactory: 'TestArrayFactory'})).toBeAn('object');
52+
});
53+
54+
it('should accept a factory name for objectFactory', function() {
55+
var ref = new Firebase('Mock://');
56+
var app = angular.module('firebase');
57+
app.provider('TestObjectFactory', {
58+
$get: function() {
59+
return function() {}
60+
}
61+
});
62+
// if this does not throw an error we are fine
63+
expect($firebase(ref, {objectFactory: 'TestObjectFactory'})).toBeAn('object');
64+
});
65+
66+
it('should throw an error if factory name for arrayFactory does not exist', function() {
67+
var ref = new Firebase('Mock://');
68+
expect(function() {
69+
$firebase(ref, {arrayFactory: 'notarealarrayfactorymethod'})
70+
}).toThrowError();
71+
});
72+
73+
it('should throw an error if factory name for objectFactory does not exist', function() {
74+
var ref = new Firebase('Mock://');
75+
expect(function() {
76+
$firebase(ref, {objectFactory: 'notarealobjectfactorymethod'})
77+
}).toThrowError();
78+
});
3579
});
3680

3781
describe('$ref', function() {

0 commit comments

Comments
 (0)