Scott Sword
Scott Sword

Reputation: 4708

Fetching Firebase Data in an AngularFire callback

Every example I have ever seen of an Angular app with Firebase has had a very static path.

Click Add -> Takes you to a New View

Click Save -> Saves Data and Routes you to a list view

Click on Item (from list view) -> Routes you to edit view of that object using $routeparams

I'm trying to write a function that adds a new object to a fb collection AND route's the user to an edit view of that particular object and in one action. AngularFire's add() method takes a callback, if I could get the $id for the object that was just added in that callback I could use $routeparams to perform the desired action.

Is this even possible?

*Update: I'm using the AngularFire method .add() which turns out was modified to return the new data reference. How is this data actually access though?

e.g. $scope.object.add(localObject);

Upvotes: 0

Views: 661

Answers (1)

Jeff Ilse
Jeff Ilse

Reputation: 366

if you use push(), name() will give you the id generated by FB.

var ref = new Firebase('https://<yourfb>.firebaseio.com/somepath');
var name = ref.push($scope.currentObject).name();
$location.path('/somepath/' + name);

Upvotes: 2

Related Questions