ElHacker
ElHacker

Reputation: 1687

Access Dynamic Parameters EmberJS

I want to update a view once a user is logged. Getting his profile picture, name, etc from a server and use that information to refresh the view. And I don't want to change the route I'm in.

The way I'm handling the logging in is by using a boolean variable isLoggedIn. I change it's value to true once the user is logged in. I have a binding listening for changes on that variable which then executes a update to the actual route's model with the user info.

App.TripsRoute = Ember.Route.extend
 isLoggedInBinding: 'App.Session.isLoggedIn'

 isLoggedInChanged: (->
   @model()
 ).observes('isLoggedIn')

 model: ->
   App.Trip.find()

So the question is : Is there any way to access the dynamic parameters from inside a route's custom method to update the related model?

Note: I don't want to do this inside the route's model hook, because it is only executed when entered via URL.

Upvotes: 1

Views: 111

Answers (1)

CraigTeegarden
CraigTeegarden

Reputation: 8251

You can store a reference to the current user in App and access that to determine if a user is logged in and use the details to update the current view.

Store the currentUser:

App = Ember.Application.create({
  currentUser: null
});

This can then be get/set anywhere in your app using App.get('currentUser') or App.set('currentUser', newUser).

In your template you can check if the user is logged in or not and automatically display their information if they are logged in:

  {{#if App.currentUser }}
    <img {{bindAttr src='App.currentUser.avatarURL'}} /> </br>
    Logged in as: {{App.currentUser.name}}
  {{else}}
    Not logged in.
  {{/if}}

Once you update the App.currentUser with the current user's user object Ember's data bindings will update your view will automatically with the profile information.

Example in JSBin

Upvotes: 1

Related Questions