I'm sure there is a much easier way to accomplish my current task but this is the only way I know how to do so far since I am just starting out with Ember.
So I have a RESTful api which tells ember which user is currently logged in (their name only) so that it can be displayed on the page as Signed in As {{currentUser}}
. The only way I have been able to make it work is by extending the application route as such:
App.ApplicationRoute = Ember.Route.extend({
model: function() {
var deferredData = Ember.Deferred.create();
var list = [];
$.getJSON( '/user/profile/', function(data) {
list.push({
currentUser: data[0].name
})
deferredData.resolve(list);
});
return deferredData;
}
});
and then on my page doing:
<p class="navbar-text navbar-right">
<p class="navbar-text navbar-right">Signed in as {{#link-to 'account' class="navbar-link" id="current-user"}}{{#each}}{{currentUser}}{{/each}}{{/link-to}}</p>
</p>
It works but I feel as if this is not the appropriate way of accomplishing my task. I tried using App.ApplicationController
with the following:
App.ApplicationControler = Ember.Controller.extend({
currentUser: function() {
$.getJSON( '/user/profile/', function(data) {
return data[0].name
});
}.property()
});
That didn't work for me either. Any ideas what I should do/what I'm doing wrong?
UPDATE
After some more reading around this is now what I have and I think it's the closest I've come to getting it right
App.ApplicationController = Ember.Controller.extend({
currentUser: function() {
return $.getJSON('/user/profile/').then( function(response) {
console.log(response[0].name);
return response[0].name;
});
}.property()
});
App.applicationController = App.ApplicationController.create();
Would using data stores and RESTadapters
be a better choice for me at this point?