This seems like a fine solution. I would still recommend checking out
routes and resolve--quite a bit more elegant as you don't need auth logic
inside your service this way.
For example, if auth is lost at some point, your Post service (which is a
singleton is going to be lost and cannot be recovered since there is no way
to re-establish). This is a side-effect of this superfluous wrapping Post
service with an `all` method that returns a predefined singleton.
Personally, I have a great deal better luck with services constructed
closer to the angularFire-seed
<
https://github.com/firebase/angularFire-seed> strategy, where we just
check auth at the route level, so it's never a concern, and return a
function from our services that can be called multiple times.
You can still reduce some of this coupling without routes/resolves, if
those are not your cup of tea.
https://gist.github.com/katowulf/eb8f9c24629f6a04464bOf course, this is all theory and preference. Just hoping to provide some
ideas that may save work later.
Cheers,
Kato
On Tue, Jan 13, 2015 at 7:56 AM, cbenerofe wrote:Kato,
thanks for the response. i somehow thought the data would be
automatically re-synced upon authorization.
i updated my factories as below, and now it's working like i had hoped.
http://jsfiddle.net/cbenerofe/Lkbj7q1d/app.factory('Post',['$firebase', 'FIREBASE_URL', 'Auth',
function ($firebase, FIREBASE_URL, Auth) {
var sync;
var posts;
Auth.$onAuth( function(authData) {
if (authData) {
sync = $firebase(new Firebase(FIREBASE_URL + '/posts'));
posts = sync.$asArray();
}
});
var Post = {
all: function() {
return posts;
},
};
return Post;
}]);
app.factory('Auth', ['$firebase', '$firebaseAuth', 'FIREBASE_URL' ,
function($firebase, $firebaseAuth, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL);
return $firebaseAuth(ref);
}
]);
On Monday, January 12, 2015 at 1:10:54 PM UTC-5, Michael Kato Wulf wrote:Hello Craig,
I don't see how this could be related to a controller vs a service--it
functions the same in both cases. More likely, you changed the timing of
the events when you refactored this code.
If the data cannot be accessed until after the user is authenticated,
then you'll need to wait until they authenticate to synchronize your array.
Otherwise it's going to cause an error and no data will be synced.
One simple wait to do this is to use $firebaseAuth::$waitForAuth, which
returns a promise that will resolve when the user is authenticated.
A better approach would be to use resolve
<
https://www.firebase.com/docs/web/libraries/angular/guide.html#section-routes>,
which is described in the AF guide
<
https://www.firebase.com/docs/web/libraries/angular/guide.html>--I
highly recommend you save yourself a ton of questions and thrashing and
digest that guide first. The resolve approach, specifically, is a great
abstraction as this avoids a bunch of if( authenticated ) then { ... } else
{ ... } logic.
Cheers,
Kato
On Thu, Jan 8, 2015 at 7:07 AM, cbenerofe wrote:here is the issue reproduced on fiddle
http://jsfiddle.net/cbenerofe/0kzwsyno/
you can login with email = te...@example.com password = test
in the PostsCtrl controller there are two ways to set the $scope.posts
variable
when set with the $firebase in the controller, the data shows up after
logging in.
when set using the Post factory, then the fiddle needs to be re-run
after logging in to have the data show up
thanks for any help, i am looking forward to insight into what is going
on.
On Tuesday, January 6, 2015 1:05:39 PM UTC-5, cbenerofe wrote:
i hope this question makes sense...
considering data in firebase under the path "firebase_url/posts" and a
rule ".read": "auth != null"
where my goal is to have no data show up pre-authentication, and then
have the data to show up after login
all works well if i attach $scope.posts to the data using $firebase and
$asArray in a controller
however, if i move the $firebase ref and $asArray into a factory, and
access the factory from my controller,
then the data does not populate on login, i have to manually refresh
the browser after logging in to get the data to show up.
thanks for any help,
--
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/9a7dd58c-5ec9-43a2-ae36-
116c1a532a37%40googlegroups.com
<
https://groups.google.com/d/msgid/firebase-angular/9a7dd58c-5ec9-43a2-ae36-116c1a532a37%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/f049ce15-2bb2-4a4e-a31b-f44199a7c2e4%40googlegroups.com<
https://groups.google.com/d/msgid/firebase-angular/f049ce15-2bb2-4a4e-a31b-f44199a7c2e4%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/CADypTEZnJgxRGwdAMYbW2NMka88z7dS9sO%2BBym-yF5_7Fu%3DgvA%40mail.gmail.com.For more options, visit
https://groups.google.com/d/optout.