Dor,

$loaded returns a promise, which is an asynchronous function call. The
return value is not instantly available in async functions. Also, you are
returning the value from an inner function which, even if the op was
synchronous, would not affect the outer find function (find isn't actually
returning anything, which is why it's undefined). These are fundamentals of
JavaScript scoping and async function calls. If you are struggling here,
you'll want to study up on asynchronous ops before you try to tackle
Angular + Firebase + Async all in one go.

On the positive side, it's not really the find function's job to deal with
this sort of log anyway. Your service shouldn't be responsible for knowing
about page load status or anything but its singular job of managing posts

To correct this, put the $loaded call in your controller, not in your find
function.

A better example of using AngularFire 0.8.
<https://gist.github.com/katowulf/cba7e5215e5d30e1a185>

Last but not least, this is all superfluous for the use case you've
provided. If you want one post, just get one post and skip the extra work
<https://gist.github.com/katowulf/220a3527272b8d31f096>.




On Thu, Aug 7, 2014 at 4:53 PM, Dor Cohen wrote:

Thanks, but I still don't understand something,
It works if I fetch the data with loaded() on the *controller* :

var ref = new Firebase(FIREBASE_URL + 'posts');
var posts = $firebase(ref).$asObject();

posts.$loaded().then(function(){
$scope.post = posts.$getRecord($routeParams.postid);
});

but when I try to implement it on the find function in the *factory* like
this:
find: function(postId) {
posts.$loaded().then(function () {
console.log(posts.$getRecord(postId));
return posts.$getRecord(postId);
});
}

and use it instead the above example, in the controller:
$scope.post = Post.find(postid);

it logs the right post, but it returns undefined, why that happens and
what is the correct way to implement this?
I guess it takes some time to fetch the data, so is there another way to
do this in the factory?

Thanks!

On Thursday, August 7, 2014 8:45:51 PM UTC+3, Michael Kato Wulf wrote:

Hey Dor,

Try posts.$getRecord(postId) inside your find function. Since the data is
already in the array and synchronized, there is no reason to create a
duplicate synchronized instance.

On Thu, Aug 7, 2014 at 4:08 AM, Dor Cohen wrote:

Hey, recently I noticed angularfire removed the method child().

I would like to know what is to correct way to handle child nodes:

var ref = new Firebase(FIREBASE_URL + 'posts');
var posts = $firebase(ref);

var Posts = {
create: function(foo) {
return posts.$push({some: foo});
},
find: function(postId) {
// no child method so I need another ref?
var postref = new Firebase(FIREBASE_URL + 'posts/' + postId)
return $firebase(postref);
}
}

also, if I want to add multiple number of child nodes

e.g posts/postId/comments/...

is the only way to do this with $set ?

Thanks alot,
Dor

--
You received this message because you are subscribed to the Google
Groups "Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to firebase-angul...@googlegroups.com.
To post to this group, send email to firebase...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/
msgid/firebase-angular/e54f3ee8-90be-4c20-b5f5-
c9c52a54e999%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/e54f3ee8-90be-4c20-b5f5-c9c52a54e999%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/1688622f-aef4-4f05-b896-0fabac6aa9cc%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/1688622f-aef4-4f05-b896-0fabac6aa9cc%40googlegroups.com?utm_medium=email&utm_source=footer>
.

For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-angular/CAFHX4%3Dov%3DQuA6rBM4JZsfZtp%2B0UD4kUh5kLapw%3DVdK2Ny4oq-w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 4 | next ›
Discussion Overview
groupfirebase-angular @
postedAug 7, '14 at 3:43p
activeAug 8, '14 at 8:18p
posts4
users2

2 users in discussion

Dor Cohen: 2 posts Michael Wulf: 2 posts

People

Translate

site design / logo © 2021 Grokbase