Skip to content

Commit

Permalink
Fixes #559 - rename $FirebaseArray and $FirebaseObject to lower case …
Browse files Browse the repository at this point in the history
…first letter

Fixes #558 - make new keyword optional

FirebaseArray.js:
 - renamed $FirebaseArray to $firebaseArray
 - made new keyword optional
 - cleaned up comments at top
 - added warning/redirect for $FirebaseArray service

 FirebaseObject.js
 - renamed $FirebaseObject to $firebaseObject
 - made new keyword optional
 - added warning/redirect for $FirebaseObject service

test/* refactor for above changes
test/chat.js: replaced $inst with $ref in a message
  • Loading branch information
katowulf committed Feb 13, 2015
1 parent 0408ccc commit a34080c
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 93 deletions.
41 changes: 29 additions & 12 deletions src/FirebaseArray.js
@@ -1,12 +1,16 @@
(function() {
'use strict';
/**
* Creates and maintains a synchronized list of data. This constructor should not be
* manually invoked. Instead, one should create a $firebase object and call $asArray
* on it: <code>$firebase( firebaseRef ).$asArray()</code>;
* Creates and maintains a synchronized list of data. This is a pseudo-read-only array. One should
* not call splice(), push(), pop(), et al directly on this array, but should instead use the
* $remove and $add methods.
*
* Internally, the $firebase object depends on this class to provide 5 $$ methods, which it invokes
* to notify the array whenever a change has been made at the server:
* It is acceptable to .sort() this array, but it is important to use this in conjunction with
* $watch(), so that it will be re-sorted any time the server data changes. Examples of this are
* included in the $watch documentation.
*
* Internally, the $firebase object depends on this class to provide several $$ (i.e. protected)
* methods, which it invokes to notify the array whenever a change has been made at the server:
* $$added - called whenever a child_added event occurs
* $$updated - called whenever a child_changed event occurs
* $$moved - called whenever a child_moved event occurs
Expand All @@ -22,10 +26,10 @@
*
* Instead of directly modifying this class, one should generally use the $extend
* method to add or change how methods behave. $extend modifies the prototype of
* the array class by returning a clone of $FirebaseArray.
* the array class by returning a clone of $firebaseArray.
*
* <pre><code>
* var ExtendedArray = $FirebaseArray.$extend({
* var ExtendedArray = $firebaseArray.$extend({
* // add a new method to the prototype
* foo: function() { return 'bar'; },
*
Expand All @@ -43,7 +47,7 @@
* var list = new ExtendedArray(ref);
* </code></pre>
*/
angular.module('firebase').factory('$FirebaseArray', ["$log", "$firebaseUtils",
angular.module('firebase').factory('$firebaseArray', ["$log", "$firebaseUtils",
function($log, $firebaseUtils) {
/**
* This constructor should probably never be called manually. It is used internally by
Expand All @@ -54,14 +58,17 @@
* @constructor
*/
function FirebaseArray(ref) {
if( !(this instanceof FirebaseArray) ) {
return new FirebaseArray(ref);
}
var self = this;
this._observers = [];
this.$list = [];
this._ref = ref;
this._sync = new ArraySyncManager(this);

$firebaseUtils.assertValidRef(ref, 'Must pass a valid Firebase reference ' +
'to $FirebaseArray (not a string or URL)');
'to $firebaseArray (not a string or URL)');

// indexCache is a weak hashmap (a lazy list) of keys to array indices,
// items are not guaranteed to stay up to date in this list (since the data
Expand Down Expand Up @@ -532,7 +539,7 @@
*/
_assertNotDestroyed: function(method) {
if( this._isDestroyed ) {
throw new Error('Cannot call ' + method + ' method on a destroyed $FirebaseArray object');
throw new Error('Cannot call ' + method + ' method on a destroyed $firebaseArray object');
}
}
};
Expand All @@ -548,7 +555,7 @@
* methods to add onto the prototype.
*
* <pre><code>
* var ExtendedArray = $FirebaseArray.$extend({
* var ExtendedArray = $firebaseArray.$extend({
* // add a method onto the prototype that sums all items in the array
* getSum: function() {
* var ct = 0;
Expand All @@ -557,7 +564,7 @@
* }
* });
*
* // use our new factory in place of $FirebaseArray
* // use our new factory in place of $firebaseArray
* var list = new ExtendedArray(ref);
* </code></pre>
*
Expand Down Expand Up @@ -672,4 +679,14 @@
return FirebaseArray;
}
]);

/** @deprecated */
angular.module('firebase').factory('$FirebaseArray', ['$log', '$firebaseArray',
function($log, $firebaseArray) {
return function() {
$log.warn('$FirebaseArray has been renamed. Use $firebaseArray instead.');
return $firebaseArray.apply(null, arguments);
};
}
]);
})();
23 changes: 18 additions & 5 deletions src/FirebaseObject.js
Expand Up @@ -14,15 +14,15 @@
* method to add or change how methods behave:
*
* <pre><code>
* var ExtendedObject = $FirebaseObject.$extend({
* var ExtendedObject = $firebaseObject.$extend({
* // add a new method to the prototype
* foo: function() { return 'bar'; },
* });
*
* var obj = new ExtendedObject(ref);
* </code></pre>
*/
angular.module('firebase').factory('$FirebaseObject', [
angular.module('firebase').factory('$firebaseObject', [
'$parse', '$firebaseUtils', '$log',
function($parse, $firebaseUtils, $log) {
/**
Expand All @@ -33,6 +33,9 @@
* @constructor
*/
function FirebaseObject(ref) {
if( !(this instanceof FirebaseObject) ) {
return new FirebaseObject(ref);
}
// These are private config props and functions used internally
// they are collected here to reduce clutter in console.log and forEach
this.$$conf = {
Expand Down Expand Up @@ -266,14 +269,14 @@
* `objectFactory` parameter:
*
* <pre><code>
* var MyFactory = $FirebaseObject.$extend({
* var MyFactory = $firebaseObject.$extend({
* // add a method onto the prototype that prints a greeting
* getGreeting: function() {
* return 'Hello ' + this.first_name + ' ' + this.last_name + '!';
* }
* });
*
* // use our new factory in place of $FirebaseObject
* // use our new factory in place of $firebaseObject
* var obj = $firebase(ref, {objectFactory: MyFactory}).$asObject();
* </code></pre>
*
Expand Down Expand Up @@ -412,7 +415,7 @@
ref.on('value', applyUpdate, error);
ref.once('value', function(snap) {
if (angular.isArray(snap.val())) {
$log.warn('Storing data using array indices in Firebase can result in unexpected behavior. See https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase for more information. Also note that you probably wanted $FirebaseArray and not $FirebaseObject.');
$log.warn('Storing data using array indices in Firebase can result in unexpected behavior. See https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase for more information. Also note that you probably wanted $firebaseArray and not $firebaseObject.');
}

initComplete(null);
Expand Down Expand Up @@ -454,4 +457,14 @@
return FirebaseObject;
}
]);

/** @deprecated */
angular.module('firebase').factory('$FirebaseObject', ['$log', '$firebaseObject',
function($log, $firebaseObject) {
return function() {
$log.warn('$FirebaseObject has been renamed. Use $firebaseObject instead.');
return $firebaseObject.apply(null, arguments);
};
}
]);
})();
2 changes: 1 addition & 1 deletion src/firebase.js
Expand Up @@ -6,7 +6,7 @@
/** @deprecated */
.factory("$firebase", function() {
return function() {
throw new Error('$firebase has been removed. You may instantiate $FirebaseArray and $FirebaseObject ' +
throw new Error('$firebase has been removed. You may instantiate $firebaseArray and $firebaseObject ' +
'directly now. For simple write operations, just use the Firebase ref directly. ' +
'See the AngularFire 1.0.0 changelog for details: https://www.firebase.com/docs/web/libraries/angular/changelog.html');
};
Expand Down
8 changes: 4 additions & 4 deletions src/utils.js
Expand Up @@ -2,8 +2,8 @@
'use strict';

angular.module('firebase')
.factory('$firebaseConfig', ["$FirebaseArray", "$FirebaseObject", "$injector",
function($FirebaseArray, $FirebaseObject, $injector) {
.factory('$firebaseConfig', ["$firebaseArray", "$firebaseObject", "$injector",
function($firebaseArray, $firebaseObject, $injector) {
return function(configOpts) {
// make a copy we can modify
var opts = angular.extend({}, configOpts);
Expand All @@ -16,8 +16,8 @@
}
// extend defaults and return
return angular.extend({
arrayFactory: $FirebaseArray,
objectFactory: $FirebaseObject
arrayFactory: $firebaseArray,
objectFactory: $firebaseObject
}, opts);
};
}
Expand Down
12 changes: 5 additions & 7 deletions tests/protractor/chat/chat.js
@@ -1,20 +1,18 @@
var app = angular.module('chat', ['firebase']);
app.controller('ChatCtrl', function Chat($scope, $FirebaseObject, $FirebaseArray) {
app.controller('ChatCtrl', function Chat($scope, $firebaseObject, $firebaseArray) {
// Get a reference to the Firebase
var chatFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/chat');
var messagesFirebaseRef = chatFirebaseRef.child("messages").limitToLast(2);

// Get AngularFire sync objects

// Get the chat data as an object
$scope.chat = new $FirebaseObject(chatFirebaseRef);
$scope.chat = $firebaseObject(chatFirebaseRef);

// Get the chat messages as an array
$scope.messages = new $FirebaseArray(messagesFirebaseRef);
$scope.messages = $firebaseArray(messagesFirebaseRef);

// Verify that $inst() works
verify($scope.chat.$ref() === chatFirebaseRef, "Something is wrong with $FirebaseObject.$inst().");
verify($scope.messages.$ref() === messagesFirebaseRef, "Something is wrong with $FirebaseArray.$inst().");
verify($scope.chat.$ref() === chatFirebaseRef, "Something is wrong with $firebaseObject.$ref().");
verify($scope.messages.$ref() === messagesFirebaseRef, "Something is wrong with $firebaseArray.$ref().");

// Initialize $scope variables
$scope.message = "";
Expand Down
12 changes: 6 additions & 6 deletions tests/protractor/priority/priority.js
@@ -1,13 +1,13 @@
var app = angular.module('priority', ['firebase']);
app.controller('PriorityCtrl', function Chat($scope, $FirebaseArray, $FirebaseObject) {
app.controller('PriorityCtrl', function Chat($scope, $firebaseArray, $firebaseObject) {
// Get a reference to the Firebase
var messagesFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/priority');

// Get the chat messages as an array
$scope.messages = new $FirebaseArray(messagesFirebaseRef);
$scope.messages = $firebaseArray(messagesFirebaseRef);

// Verify that $inst() works
verify($scope.messages.$ref() === messagesFirebaseRef, 'Something is wrong with $FirebaseArray.$ref().');
verify($scope.messages.$ref() === messagesFirebaseRef, 'Something is wrong with $firebaseArray.$ref().');

// Initialize $scope variables
$scope.message = '';
Expand All @@ -27,17 +27,17 @@ app.controller('PriorityCtrl', function Chat($scope, $FirebaseArray, $FirebaseOb
from: $scope.username,
content: $scope.message
}).then(function (ref) {
var newItem = new $FirebaseObject(ref);
var newItem = $firebaseObject(ref);

newItem.$loaded().then(function (data) {
verify(newItem === data, '$FirebaseObject.$loaded() does not return correct value.');
verify(newItem === data, '$firebaseObject.$loaded() does not return correct value.');

// Update the message's priority
newItem.$priority = priority;
newItem.$save();
});
}, function (error) {
verify(false, 'Something is wrong with $FirebaseArray.$add().');
verify(false, 'Something is wrong with $firebaseArray.$add().');
});

// Reset the message input
Expand Down
6 changes: 3 additions & 3 deletions tests/protractor/tictactoe/tictactoe.js
@@ -1,16 +1,16 @@
var app = angular.module('tictactoe', ['firebase']);
app.controller('TicTacToeCtrl', function Chat($scope, $FirebaseObject) {
app.controller('TicTacToeCtrl', function Chat($scope, $firebaseObject) {
// Get a reference to the Firebase
var boardFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/tictactoe');

// Get the board as an AngularFire object
$scope.boardObject = new $FirebaseObject(boardFirebaseRef);
$scope.boardObject = $firebaseObject(boardFirebaseRef);

// Create a 3-way binding to Firebase
$scope.boardObject.$bindTo($scope, 'board');

// Verify that $inst() works
verify($scope.boardObject.$ref() === boardFirebaseRef, 'Something is wrong with $FirebaseObject.$ref().');
verify($scope.boardObject.$ref() === boardFirebaseRef, 'Something is wrong with $firebaseObject.$ref().');

// Initialize $scope variables
$scope.whoseTurn = 'X';
Expand Down
8 changes: 4 additions & 4 deletions tests/protractor/todo/todo.js
@@ -1,13 +1,13 @@
var app = angular.module('todo', ['firebase']);
app. controller('TodoCtrl', function Todo($scope, $FirebaseArray) {
app. controller('TodoCtrl', function Todo($scope, $firebaseArray) {
// Get a reference to the Firebase
var todosFirebaseRef = new Firebase('https://angularFireTests.firebaseio-demo.com/todo');

// Get the todos as an array
$scope.todos = new $FirebaseArray(todosFirebaseRef);
$scope.todos = $firebaseArray(todosFirebaseRef);

// Verify that $ref() works
verify($scope.todos.$ref() === todosFirebaseRef, "Something is wrong with $FirebaseArray.$ref().");
verify($scope.todos.$ref() === todosFirebaseRef, "Something is wrong with $firebaseArray.$ref().");

/* Clears the todos Firebase reference */
$scope.clearRef = function () {
Expand Down Expand Up @@ -35,7 +35,7 @@ app. controller('TodoCtrl', function Todo($scope, $FirebaseArray) {
/* Removes the todo item with the inputted ID */
$scope.removeTodo = function(id) {
// Verify that $indexFor() and $keyAt() work
verify($scope.todos.$indexFor($scope.todos.$keyAt(id)) === id, "Something is wrong with $FirebaseArray.$indexFor() or FirebaseArray.$keyAt().");
verify($scope.todos.$indexFor($scope.todos.$keyAt(id)) === id, "Something is wrong with $firebaseArray.$indexFor() or FirebaseArray.$keyAt().");

$scope.todos.$remove(id);
};
Expand Down

0 comments on commit a34080c

Please sign in to comment.