我是 ember js 的新手,我正在尝试了解路由:)
这是我的 Controller 代码:
App.AppController = Em.ArrayController.extend({
login: App.Login.create({}),
loginUser: function(){
$.ajax({
type: 'POST',
cache: false,
url: contextPath + 'user/register',
data:{name:this.login.name, email:this.login.email,password:this.login.password,confirmPassword:this.login.confirmPassword,acceptTerms:this.login.acceptTerms},
success: function(data) {
alert('success!');
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
}
})
这里是我使用函数的地方:
App.ChooseServiceIndexRoute = Em.Route.extend({
redirect: function() {
var isLoggedIn = this.controllerFor('app').loginUser();
alert(isLoggedIn);
if(isLoggedIn){
this.transitionTo('chooseService');
}
else {
this.transitionTo('app');
}
}
});
看起来,函数 loginUser 被调用了,但是 alert(isLoggedIn) 发生在函数完成之前。(Alert(success) 发生在 alert(isLoggedIn) 之后。
我可以借助一些帮助来了解正在发生的事情。
谢谢!
最佳答案
Ajax 表示异步 Javascript 和 XML,因此根据定义它是异步的,这意味着它不会停止代码直到完成,而是在加载完成后运行回调。您可以将 async 设置为 false,这样它将停止执行直到完成。你必须小心超时和类似的情况。
在此处查看可用选项: $.ajax documentation
关于javascript - Ember : In my example why does the logic in the router continue even though the function used in it has not completed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15580767/