This migration guide will walk through some of the major breaking changes with code samples and
guidance to upgrade your existing application from AngularFire version 0.9.x
to 1.x.x
.
The largest breaking change in AngularFire 1.0.0
is the removal of the $firebase
service. The
service did not provide any capabilities beyond what already existed in the vanilla Firebase SDK.
However, sometimes you do need to quickly write data from your database without first going through
the process of creating a synchronized array or object. Let's walk through some examples of
migrating away from the now defunct $firebase
service.
To write data to your database, we should now use the Firebase SDK's set()
method instead of the
removed $set()
method.
app.controller("SampleCtrl", ["$scope", "$firebase",
function($scope, $firebase) {
var profileRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/profiles/annie");
var profileSync = $firebase(profileRef);
profileSync.$set({ age: 24, gender: "female" }).then(function() {
console.log("Profile set successfully!");
}).catch(function(error) {
console.log("Error:", error);
});
}
]);
app.controller("SampleCtrl", ["$scope",
function($scope) {
var profileRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/profiles/annie");
profileRef.set({ age: 24, gender: "female" }, function(error) {
if (error) {
console.log("Error:", error);
} else {
console.log("Profile set successfully!");
}
});
}
]);
We should similarly use the Firebase SDK's remove()
method to easily replace the $remove()
method provided by the $firebase
service.
app.controller("SampleCtrl", ["$scope", "$firebase",
function($scope, $firebase) {
var profileRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/profiles/bobby");
var profileSync = $firebase(profileRef);
profileSync.$remove().then(function() {
console.log("Set successful!");
}).catch(function(error) {
console.log("Error:", error);
});
}
]);
app.controller("SampleCtrl", ["$scope",
function($scope) {
var profileRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/profiles/bobby");
profileRef.remove(function(error) {
if (error) {
console.log("Error:", error);
} else {
console.log("Profile removed successfully!");
}
});
}
]);
Replacements for the $asArray()
and $asObject()
methods are given below.
Due to the removal of $firebase
, the process of creating an instance of a synchronized object has
changed. Instead of creating an instance of the $firebase
service and calling its $asObject()
method, use the renamed $firebaseObject
service directly.
app.controller("SampleCtrl", ["$scope", "$firebase",
function($scope, $firebase) {
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var sync = $firebase(ref);
var obj = sync.$asObject();
}
]);
// Inject $firebaseObject instead of $firebase
app.controller("SampleCtrl", ["$scope", "$firebaseObject",
function($scope, $firebaseObject) {
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
// Pass the Firebase reference to $firebaseObject directly
var obj = $firebaseObject(ref);
}
]);
Due to the removal of $firebase
, the process of creating an instance of a synchronized array has
changed. Instead of creating an instance of the $firebase
service and calling its $asArray()
method, use the renamed $firebaseArray
service directly.
app.controller("SampleCtrl", ["$scope", "$firebase",
function($scope, $firebase) {
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var sync = $firebase(ref);
var list = sync.$asArray();
}
]);
// Inject $firebaseArray instead of $firebase
app.controller("SampleCtrl", ["$scope", "$firebaseArray",
function($scope, $firebaseArray) {
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
// Pass the Firebase reference to $firebaseArray
var list = $firebaseArray(ref);
}
]);
Due to the removal of $firebase
, the $inst()
methods off of the old $FirebaseObject
and
$FirebaseArray
factories were no longer meaningful. They have been replaced with $ref()
methods
off of the new $firebaseObject
and $firebaseArray
services which return the underlying
Firebase
reference used to instantiate an instance of the services.
// $FirebaseObject
var objSync = $firebase(ref);
var obj = sync.$asObject();
objSync === obj.$inst(); // true
// $FirebaseArray
var listSync = $firebase(ref);
var list = sync.$asArray();
listSync === list.$inst(); // true
// $firebaseObject
var obj = $firebaseObject(ref);
obj.$ref() === ref; // true
// $firebaseArray
var list = $firebaseArray(ref);
list.$ref() === ref; // true
The previously deprecated ability to pass in credentials to the user management methods of
$firebaseAuth
as individual arguments has been removed in favor of a single credentials argument
var auth = $firebaseAuth(ref);
auth.$changePassword("[email protected]", "somepassword", "otherpassword").then(function() {
console.log("Password changed successfully!");
}).catch(function(error) {
console.error("Error: ", error);
});
var auth = $firebaseAuth(ref);
auth.$changePassword({
email: "[email protected]",
oldPassword: "somepassword",
newPassword: "otherpassword"
}).then(function() {
console.log("Password changed successfully!");
}).catch(function(error) {
console.error("Error: ", error);
});
The $sendPasswordResetEmail()
method has been removed in favor of the functionally equivalent
$resetPassword()
method.
var auth = $firebaseAuth(ref);
auth.$sendPasswordResetEmail("[email protected]").then(function() {
console.log("Password reset email sent successfully!");
}).catch(function(error) {
console.error("Error: ", error);
});
var auth = $firebaseAuth(ref);
auth.$resetPassword("[email protected]").then(function() {
console.log("Password reset email sent successfully!");
}).catch(function(error) {
console.error("Error: ", error);
});