Giter Club home page Giter Club logo

Comments (12)

bennlich avatar bennlich commented on June 5, 2024

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.

lifeisfoo avatar lifeisfoo commented on June 5, 2024

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.

anantn avatar anantn commented on June 5, 2024

@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.

bennlich avatar bennlich commented on June 5, 2024

@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.

anantn avatar anantn commented on June 5, 2024

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.

bennlich avatar bennlich commented on June 5, 2024

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.

lifeisfoo avatar lifeisfoo commented on June 5, 2024

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.

anantn avatar anantn commented on June 5, 2024

It won't work with angularFireCollection, you need to use angularFire if you want to reference objects that way.

from angularfire.

allumbra avatar allumbra commented on June 5, 2024

@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.

allumbra avatar allumbra commented on June 5, 2024

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.

anantn avatar anantn commented on June 5, 2024

@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.

anantn avatar anantn commented on June 5, 2024

We added getByName and getByNames to angularFireCollection which should make both angularFire and angularFireCollection key-addressable.

from angularfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.