0

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?

3
  • How do you will do the authentication logic? Using some framework or by hand etc. Commented Nov 21, 2013 at 15:03
  • I'm doing it by hand, similar to this: log.simplabs.com/post/53016599611/authentication-in-ember-js Commented Nov 21, 2013 at 15:19
  • it would go in App.SessionsNewRoute createSession action then. Commented Nov 21, 2013 at 16:56

3 Answers 3

1

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('')

Sign up to request clarification or add additional context in comments.

1 Comment

I did something very similar where my login controller would dispatch an event which is catched at the ApplicationRoute. Thanks!
0

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
  }
});

2 Comments

What happens when I'm not authenticated when the app boots but I sign in after that? A sign in doesn't cause an page load so the boot logic already happened.
ah yes - this would require another type of action driven by another component
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.