0

I have searched and experimented and nothing seems to work the way I want it to. If anyone has any suggestions please post any ideas.

So this is how I want it to work.

var myfunc = function(args) {
  var result = null;
  result = __myservice.$loaded().then(function(data) {
    return data;
  });

  return result;
}

This returns NULL every time if anyone knows how to do this please let me know.

edit:(added service/factory code)

this is using angularfire seed fyi with most of the code unchanged so the fbutil is the one from angularseed which I believe has the Auth built in as well.

app.factory('circlesList', ['fbutil', '$firebaseArray', function(fbutil,  $firebaseArray) {
  var ref = fbutil.ref('circles');
  return $firebaseArray(ref);
}]);
  • Do you check Auth before the request? can you share you'r __myservice code? – giladk Nov 12 '15 at 15:50
  • I wrote an answer below to explain why you're seeing this behavior. But this is very much an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). To allow us to best help you, state the goal you're trying to achieve, not just the means in which you're trying to achieve it. – Frank van Puffelen Nov 12 '15 at 16:36
  • I understand that firebase returns an empty array as a promise or whatever and then gets the data and that it's async so the first go round doesn't get the data at the same time as the code gets executed in the queue. I'm just hoping theres some way to do it without having to manipulate it from the view and back into the controller like I've had to do to fix this issue for now. I hope that explains the reason I'm trying to return the data loaded from the .then().... think of it as I'm trying to manipulate the data without it ever hitting the DOM – Samuel Barney Nov 12 '15 at 16:53
  • If you're looking to manipulate data that is being loading into, updated in or removed from the `$firebaseArray`: http://stackoverflow.com/questions/33662141/rendering-information-of-each-child-in-a-list-of-a-denormalized-data-structure/33662752#33662752 – Frank van Puffelen Nov 12 '15 at 17:25

1 Answers1

1

The data is loaded (and synchronized) asynchronously. What you're trying is to circumvent the asynchronous nature of the modern web. But unfortunately you cannot return something now that has not come back from the server yet.

See this reference question for a great explanation: How do I return the response from an asynchronous call?

You could instead return the $loaded() promise, which allows the calling function to do the then() part.

Note though that $loaded() likely only fires when the initial data has loaded. When you use Firebase it is best to let go of this request/response mindset and instead rephrase your goals in terms of "whenever this data changes, I want to do ABC".

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So should I be doing something like $on 'change' type deal instead of using $loaded??? – Samuel Barney Nov 12 '15 at 16:50
  • Yup. It's called extending `$firebaseArray`. I'll recently answered that already, so will link that one instead of repeating: http://stackoverflow.com/questions/33662141/rendering-information-of-each-child-in-a-list-of-a-denormalized-data-structure/33662752#33662752. – Frank van Puffelen Nov 12 '15 at 17:24
  • I read that in the docs already but I think it didn't seem like what I needed, I'll revisit that and test it out. So if I'm correct what your saying is this will ensure it loads the data at the factory level before hitting the controller then? So in the controller I won't need to use any $loaded/$on or any watcher type issues as it will be able to manipulate it the first time it comes back to the controller from the factory then? Fingers crossed before I test it but I think this is the closest to what I'm wanting. Thanks for the help Frank. – Samuel Barney Nov 12 '15 at 17:33
  • I ended up just loading/defining a root scope after auth then a route reload if it's not loaded initially after first route change then all my manipulations inside the controller are returning loaded data values the correct way as if it's from a static factory then hitting the DOM after that. It's a bit of a Jerry rigging but nothing else worked for me. I'm sure your solution works I just prob did it wrong in my test that failed. Thanks for the effort tho – Samuel Barney Nov 12 '15 at 22:12