Comments (12)
Looks like I misunderstood your question, lifeisfoo. Also, the example I posted above won't work (and is now deleted). It would be nice to be able to treat an angularFireCollection like a dictionary, but under the hood it's really an Array.
In your example, invite
will contain "mydata," so you could do something like this:
<li ng-repeat="invite in invites">
<a href="#" ng-click="enterChat(invite.$id)">{{ invite.$id }}</a>
<p>{{ invite.content }}</p>
</li>
This is assuming a data structure like:
invites: {
username1: {
content: "cookies"
}
}
From a controller, though, I'm not sure. Has anyone else encountered this? It looks to me like there's no public method to access an angularFireCollection element by key.
from angularfire.
I'm referring to something like this:
invite[invite.$id]
since the resource key is dynamic (not a fixed "username" string):
invites: {
"lifeisfoo": "data"
"bennlich": "otherData"
//...
}
I've tried using this information http://stackoverflow.com/questions/2559159/how-to-dynamic-call-property-on-javascript-object-with-jquery but seems to not work.
from angularfire.
@lifeisfoo, how do you determine the key that you need to access? If it's in a variable, you should simply be able to do:
var keyname = "username";
for (var index in $scope.invites) {
var invite = $scope.invites[index];
var data = invite[keyname]; // "mydata" is now in the data variable
}
from angularfire.
@anantn, I think he means that $scope.invites
is a dictionary, so he wants to access a specific invite by key, as opposed to accessing it via iteration, or via whatever index angularFire has assigned it.
from angularfire.
Ah, in that case, I'd recommend using the angularFire
function instead:
var promise = angularFire(fbInvites, $scope, 'invites', {});
promise.then(function() {
$scope.invites[keyname];
});
from angularfire.
Oh, interesting. It's probably worth noting in the docs that an angularFireCollection is not key-addressable, while an angularFire is.
The difference between the two data structures seems kind of arbitrary to me right now. Are there reasons not to have an explicitly-synced dictionary? Or should I make that a feature request (and maybe begin working on it)?
from angularfire.
Currently I use this hack to get the data associated at the key. Note that, in this case, my "invites" collection contains a maximum of one invite.
var roomIDhack = "";
angular.forEach($scope.invites, function(value, key){
for (var i = 0; value[i] != null; i++) {
roomIDhack+=value[i];
}
});
@anantn I tried with your first example (using angularFireCollection), but isn't working:
for (var index in $scope.invites) {
var invite = $scope.invites[index];
var data = invite[invite.$id]; // the key is the $id
}
@bennlich Yes, invites is a a dictionary (see my previous post for an example).
from angularfire.
It won't work with angularFireCollection
, you need to use angularFire
if you want to reference objects that way.
from angularfire.
@anantn I am also struggling with this. I can read data using the angularFire notes above, but my writes are not being committed. I am relatively new to AngularJS and very new to firebase. The following code is meant to write a user at userId = "user1".
var url = "https://mbgl.firebaseio.com/users";
console.log("url: " + url);
var promise = angularFire(url, $scope, 'users', {});
promise.then(function (users) {
$scope.users["user1"] = {first: "Mr.", last: "BoJangles"};
console.log($scope.users["user1"]);
});
from angularfire.
I had a breakthrough - there is a timing aspect to this. I can observe changes to the firebase object with the code below. My next question is - what is the proper way of handling these timing issues?
var url = "https://mbgl.firebaseio.com/users/user1";
console.log("url: " + url);
var promise = angularFire(url, $scope, 'user', {});
promise.then(function (users) {
window.setTimeout(function () {
safeApply($scope, function () {
$scope.user = {first: "Mr.", last: "BoJangles"};
});
}, 1000)
window.setTimeout(function () {
safeApply($scope, function () {
$scope.user.last = "Goodbar";
});
}, 5000)
window.setTimeout(function () {
safeApply($scope, function () {
$scope.user = {first: "Mrs.", last: "Pacman"};
});
}, 3000)
});
from angularfire.
@allumbra Any changes to $scope
inside callbacks will need to be wrapped in $timeout
. Therefore, if you are trying to add an item inside the promise callback, you can try this:
function MyController($scope, $timeout, angularFire) {
var url = "https://mbgl.firebaseio.com/users";
var promise = angularFire(url, $scope, 'user', {});
promise.then(function() {
$timeout(function() {
$scope["user1"] = {first: "Mr.", last: "BoJangles"};
});
});
}
from angularfire.
We added getByName and getByNames to angularFireCollection which should make both angularFire and angularFireCollection key-addressable.
from angularfire.
Related Issues (20)
- Error: Metadata version mismatch for module node_modules/angularfire2/index.d.ts, found version 4, HOT 2
- JS console error displaying my API Key on failed login - Chrome only
- Your library does not support callbacks for set(), push(), while Firebase does
- Are there any plans to include ML Kit support?
- ng build --prod || Can't resolve './public_api' in '/node_modules/angularfire2/database' HOT 1
- firestorage $putString callbacks not binding HOT 2
- Firestore support HOT 1
- Dependency Update HOT 1
- I keep getting "TypeError: Right-hand side of 'instanceof' is not callable" error no matter what sign in method I try
- Firestore is loading all data in every new insert or any updates
- callable functions throw deadline-exceeded even though the function was defined with max timeout in nodes HOT 2
- New Console Error "TypeError: v12 is not a function" With Version 0.9.2 HOT 1
- 'child_added' does not trigger when re-adding same item to RTDB
- 'child_removed' triggers on setting listener, before child is removed
- ERROR Error: Uncaught (in promise): TypeError: Cannot convert undefined or null to object HOT 1
- ERROR TypeError: ZS(...).firestore is not a function HOT 1
- Email verification HOT 1
- Uncaught TypeError: Cannot read property 'GoogleAuthProvider HOT 1
- auth.signInAnonymously() support HOT 1
- Potential Security Issue
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angularfire.