Reputation: 1578
Firebase allows easy creation of arrays, but they claim to be read-only. I want to have an array of my objects that I can go in and update, or change some of their attributes.
The problem is that arrays in Firebase are created using timestamps as keys. Everything in Firebase is a URL, and I don't have those keys to put in the URL.
I'm using AngularFire and Firebase.
.controller('LinkCtrl',[ '$scope', '$firebaseArray', '$firebaseObject', '$log', function ($scope, $firebaseArray, $firebaseObject, $log) {
var ref = new Firebase('https://candyman.firebaseio.com/links');
$scope.links = $firebaseArray(ref);
$scope.addLink = function () {
// newly added
var newLinkRef = ref.push();
newLinkRef.set({ name: $scope.newLinkName, url: $scope.newLinkUrl, downloadCount: 1, timestamp: Firebase.ServerValue.TIMESTAMP });
// !newly added
$scope.newLinkName = '';
$scope.newLinkUrl = '';
$log.info($scope.newLinkName + ' added to database');
};
My goal is to create an object that is stored in my array, then at some later period be able to call a function that modifies the downloadCount
for a specific object. I can't hard code the link because I want to increase the count for any of the objects, not just one.
Upvotes: 0
Views: 899
Reputation: 1578
Turns out it isn't listed in the docs. At least, not at all up front. But when you are accessing an item in an array in your database, it ships with all of the normal attributes that you gave it (e.g. firstName
, lastName
, etc. All of the regular attributes) plus the wonderful id
attribute. id
is the timestamp that your item got when you added it to the array.
Once you get the id
(the timestamp) you can use that to reference your item (since all of your references back to your database are done via URL. So you can end up saying...
var ref = new Firebase(https://myApp.firebaseio.com/users/);
// ref now holds the string that is your URL
$scope.users = $firebaseArray(ref);
// 'user' can be passed in through $scope
$scope.updateUserName = function (user) {
var userRef = new Firebase(ref + user.$id);
// userRef now holds the string that is the URL to your user that you just passed in
// don't be scared to use dynamic, client-facing variables instead of hard-coding this. It works.
userRef.update({ firstName: 'Jacob' });
};
So, yes. It is supported. And the docs could be a bit more noob-friendly. No need to be all elite.
Upvotes: 0
Reputation: 598847
Is it really not supported?
Given that we're talking about software here, that is highly unlikely. It's more likely that the documentation is confusing you. Which is a lot easier to help with if you show some of the things you tried.
For a simple example, say that you have a database with a list of items:
var ref = new Firebase('https://yours.firebaseio.com');
var itemsRef = ref.child('items');
Now you add a new item to this list:
var newItemRef = ref.push();
newItemRef.set({ name: 'New Item', user: 'Jacob Dick', timestamp: Firebase.ServerValue.TIMESTAMP });
You can then change a single property of that item with:
newItemRef.update({ user: 'Frank van Puffelen' });
Upvotes: 2