I've got a little ember project and want to build it up right now with ember-cli (node.js) because of a better performance etc.
In my project i have the possibility to provide different layouts. Layout one is the main layout. Layout two is the login layout. Nevertheless every controller has access to the user, the layout, the config object etc. By visiting the website for the first time, my RESTAdapter (Api & ApiSync) checks whether the user is logged in or logged out. If he is logged in the variable will be set.
Note: ApiSync is an ajax request performed synchronously.
"Config" variable is set in a file before. Nothing special.
Here my code:
var App = Ember.Application.create({
loggedIn: false,
me: false,
config: Config,
detail:false,
layout:'LoginLayout',
Router: Ember.Router.reopen({
location: 'history',
rootURL: Config.root,
}),
Controller: Ember.Controller.reopen({
loggedIn: function() {
return Ember.get('App.loggedIn')
}.property('App.loggedIn'),
me: function() {
return Ember.get('App.me')
}.property('App.me'),
config: function() {
return Ember.get('App.config')
}.property('App.config'),
detail: function() {
return Ember.get('App.detail')
}.property('App.detail'),
layout: function() {
return Ember.get('App.layout')
}.property('App.layout')
}),
Route: Ember.Route.reopen({
beforeModel: function(transition) {
var loggedIn = App.get('loggedIn');
if (!loggedIn && this.needAuth != undefined && this.needAuth) {
this.transitionTo('login');
return;
}
$("#Loader").addClass("ajaxloader1");
this._super();
},
afterModel: function(model) {
this._super();
if(this.title){
var title = this.title;
if(lang[Config.language][this.title] != undefined) title = lang[Config.language][title];
$(document).attr('title', title + ' | ' + Config.title);
}
$("#Loader").removeClass("ajaxloader1");
}
})
});
App.Router.map(function() {
this.route('error');
this.route('catchAll', {path: '*:'});
});
App.CatchAllRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('error');
}
});
App.ApplicationRoute = Ember.Route.extend({
actions: {
error: function () {
this.transitionTo('error', "error");
}
}
});
ApiSync.find('user/me', {}).done(function(r){
if(r.status == 'success'){
App.set('loggedIn',true);
App.set('me',r.result);
App.set('layout','MainLayout');
}
});
But I really have no idea how to do it the same way with ember-cli. I just googled a lot but found nothing that could help me to figure it out :(
Hopefully someone can help me :)
P.S.: Maybe my code can help someone else for the question how to provide a user login ...
Note: Using Ember 1.11.0-beta.4
Initial template looks like this:
<div>
{{partial layout}}
</div>
Aucun commentaire:
Enregistrer un commentaire