0

I want to define a simple factory that will be the entry point for a $resource definition for a given resource in my app (say Post). The problem is that the URL for Post depends on the user's organization, so it doesn't make sense (nor is it even possible) to instantiate the Post class until the user has signed in (eg, a url like https://host.com/organizations/your-org/posts/:id)

The controllers that depend on Post are all protected by login and so it should be OK (in theory) to define Post assuming the user has already logged in.

However I've found that simple requiring Post in any controller signature initializes the Post factory at bootstrap time.

angular.module('myapp.services', [])
.factory("Post", function($resource, Auth, $localStorage){
  return $resource(Auth.links.posts.href, {id: "@id"});
});

Auth.links is initialized after login, so bootstrapping the app when a user is logged out causes errors because Auth.links is null.

I've thought of 2 ways to deal with this, neither of which I'm really excited about:

  1. Rewrite Post factory to return a promise that will be immediately resolved assuming you're logged in. But why create another callback when this should just work?
  2. Make the initial Post factory a stub and then modify the Auth.login() method so that it overwrites the Post factory with the correct definition. This seems hacky and non-standard, and I've read that trying to add services or factories after the app has been bootstrapped doesn't work properly (without some hacks).

Has anyone solved similar problems before? And/or do you recommend some particular approach? Thanks in advance.

EDIT:

SOOOO apparently I had an if statement incorrect in my router that was defaulting logged-out users to a logged-in page, which was causing this whole mess. Turns out that stuff does actually work how it's supposed to and my problem was coder error. Derp.

steve
  • 3,276
  • 27
  • 25
  • possible duplicate of [Initialize AngularJS service with asynchronous data](http://stackoverflow.com/questions/16286605/initialize-angularjs-service-with-asynchronous-data) – Phil May 13 '15 at 05:55
  • Yeah I guess using `resolve` in the router is an OK option. It feels kinda icky to me, because I don't want to change the name of my factory to `PostFactory` or `PostModelAndPromise` or something like that but it's definitely a better option than other things I've come up with so far. – steve May 13 '15 at 06:11
  • There are other answers in that post that might apply – Phil May 13 '15 at 06:17
  • That question in general is about running something as soon as possible, like before the app is bootstrapped. My question is about _delaying_ the instantiation of a factory until after login. Similar but unfortunately the answers in that question didn't seem applicable to me – steve May 13 '15 at 06:32
  • You can initialise it manually using $injector : $injector.get('$log'); – aorfevre May 13 '15 at 11:39
  • Something bizarre was going on yesterday. Now it's magically working, without code changes. I blame Gremlins. – steve May 13 '15 at 19:19

0 Answers0