【发布时间】:2013-12-30 12:50:21
【问题描述】:
我正在尝试在 Ember.JS 应用程序中获取当前用户(如果已登录)。
我遵循@MilkyWayJoe 在this post 中概述的一般方法。这需要在ApplcatinController 上拥有一个属性
像这样:
App.ApplicationController = Ember.Controller.extend({
current_user: null,
init: function(){
this.set('current_user', this.store.find("user", "me"));
}
});
这很好用,在用户登录时加载当前用户。但是,当用户未登录时,服务器(正确)不返回任何用户。我在控制台中收到以下错误:
Assertion failed: You made a request for a user with id me, but the adapter's response did not have any data
一切仍然有效,但我想避免在控制台中弹出错误。
所以我尝试直接使用 find 返回的承诺:
App.ApplicationController = Ember.Controller.extend({
current_user: null,
init: function(){
var user_promise = this.store.find("user", "me");
var app_controller = this;
user_promise.then(function(user){ //Success
app_controller.set('current_user', user)
}, function(reason){ //Fail
//just resolve the promise, no need to do anything
})
}
});
与第一个示例具有完全相同的行为:它可以工作,但仍将相同的错误打印到控制台中。
如何处理用户未登录而控制台中出现错误的情况?
【问题讨论】:
-
您是否尝试过使用
try..catch? -
如何覆盖适配器以始终返回某些内容(即使服务器没有返回任何内容)? stackoverflow.com/questions/17938294/…
标签: javascript json ember.js