【问题标题】:Dealing with no JSON returned in Ember.js处理 Ember.js 中没有返回的 JSON
【发布时间】: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 
   })       
 }
});

与第一个示例具有完全相同的行为:它可以工作,但仍将相同的错误打印到控制台中。

如何处理用户未登录而控制台中出现错误的情况?

【问题讨论】:

标签: javascript json ember.js


【解决方案1】:

对你来说可能为时已晚,但对于未来的搜索者来说:

DS.Store.find 接受一个承诺作为回报。如果您正在为数据服务编写自己的适配器,而不是使用您的 api 的结果调用 promise.resolve,您可以调用 promise.reject,这将由 Ember 处理,而不会在您的控制台中引发错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多