Currently, I am developing a hybrid app that involves server processing and using Ember for the main UI implementation.
All authentication functionalities are handled on the server side, so upon page load, the user's authentication status is already determined based on cookies.
In essence, on the client-side, there exists a userId
cookie, which signifies whether the user is authenticated or not.
Now, the challenge lies in making this data accessible to all templates within the application.
I managed to resolve this issue for the application template through the following code snippet (implemented in CoffeeScript):
Route:
ApplicationRoute = Ember.Route.extend
setupController: (controller) ->
userId = jQuery.cookie 'userId'
if userId == 'undefined'
userId = null
else
userId = parseInt userId, 10
controller.set 'userId', userId
Controller:
ApplicationController = Ember.Controller.extend
userId: null
Finally, the template:
<strong>
{{#if userId}}
userId: {{userI }}
{{else}}
No user
{{/if}}
</strong>
While this solution works seamlessly from the application template, any attempt to move it to another template like the index
results in displaying 'no user' (indicating issues with controller chaining).
I also experimented with converting it into a bound helper but encountered difficulties as the helper failed to execute at all:
Ember.Handlebars.registerBoundHelper 'userId', ->
userId = jQuery.cookie 'userId'
if userId == 'undefined'
userId = null
else
userId = parseInt userId, 10
userId