stravid
stravid

Reputation: 1009

Where to load common data in an application with authentication

I have an application with a sidebar which is always visible. For this sidebar I have to load common data once. This is usually not a problem, I do this in the setupController hook of the ApplicationRoute.

Tedian.ApplicationRoute = Ember.Route.extend
  setupController: ->
    controller = @controllerFor("sidebar")
    controller.set "tasks", @store.find("task")
    controller.set "projects", @store.find("project")
    controller.set "timeEntries", @store.find("timeEntry")

    Tedian.TimeEntry.findActive().then (timeEntry) ->
      controller.set "activeTimeEntry", timeEntry

But where do I put this setup code in an application with authentication?

I don't want to run this code for an un-authenticated user so I can't put it into the ApplicationRoute. Where is the best place to put it instead?

Upvotes: 0

Views: 174

Answers (3)

Eaten by a grue
Eaten by a grue

Reputation: 391

Perhaps i'm too stupid here, but i would simply check if the user is logged in:

App.ApplicationRoute = Ember.Route.extend({

   initializeSidebar: function() {
      if (!App.user.get('isSignedIn')) return;
      var controller = this.controllerFor('sidebar');
      //...
   }.observes('App.user.isSignedIn'),

   setupController: function(controller, model) {
       controller.set('model', model);
       this.initializeSidebar();
   }
});

Now, if the user signs in or the user is already signed in, but the information about that comes asynchronously, the sidebar gets updated.

I don't know if it's a good practice to store the user object directly on the App, but as you want access it from everywhere anyways, i think it's ok. Alternatively have it as an attribute of a controller that you then can access from the ApplicationRoute by calling this.controllerFor('')

Upvotes: 1

Dave-Choi
Dave-Choi

Reputation: 105

Most likely, you'll want to have a User model that has several hasMany relationships with your Task, Project and TimeEntry models. Then, when you authenticate your user, however you do that, the server response should include those related models.

Then your SidebarController can have a controller need for the current user:

App.SidebarController = Ember.Controller.extend({
    needs: ["currentUser"]
});

And your sidebar template can have something like

{{render "tasks" currentUser.tasks}}
{{render "projects" currentUser.projects}}
{{render "timeEntries" currentUser.timeEntries}}

And those will populate automatically when you set the currentUser with the appropriate data.

If you don't have control over the server response, then you can have an observer on the currentUserController fetch that stuff after authentication.

Upvotes: 0

Toran Billups
Toran Billups

Reputation: 27399

You could check to see if the user is authenticated when the app boots

App.initializer({
  name: 'bootstrap',
  initialize: function() {
    //if user is authenticated do a $.ajax or 2 in here
  }
});

Upvotes: 0

Related Questions