簡體   English   中英

灰燼模式承諾的問題

[英]Issue with Ember Model promises

我正在嘗試使用Ember Model做一些基本的事情,但是我遇到了一些帶有承諾的怪異行為。 不知道我對它應該如何工作非常了解。

所以,我有這條路線:

App.ProfilesIndexRoute = Ember.Route.extend {

  redirect: ->
    App.Profile.fetch().then (profiles) ->
      console.log profiles.get('length')
      @transitionTo 'profile', profiles.get('firstObject')

}

我只是希望人們在訪問/profiles時被重定向到/profiles/123 (=第一個配置文件)。

這是配置文件適配器:

App.Profile.adapter = Ember.Adapter.create {

  findAll: (klass, records) ->
    $.ajax('/api/users/' + $.cookie('user_id'), {
      type: 'get'
      headers: {
        'Content-Type': 'application/json'
        'Authentication-Token': $.cookie('token')
      }
      dataType: 'json'
    }).then (res) ->
      profiles = []
      // some stuff done here
      records.load klass, profiles

}

當我轉到/profiles ,這是我在控制台中看到的內容:

0
Transitioned into 'profiles.index'
XHR finished loading: "http://localhost/api/users/456". 

0console.log profiles.get('length') 看來它是在AJAX調用有機會完成之前被調用的。 我應該在控制台中輸入以下內容:

Transitioned into 'profiles.index'
XHR finished loading: "http://localhost/api/users/456". 
5

我在這里做錯了什么? 這里有人建議我使用fetch而不是find但這似乎不能解決我的問題,因為事情沒有按正確的順序執行。

謝謝!

您應該從afterModel掛鈎重定向到其他路由:

App.ProfilesIndexRoute = Ember.Route.extend {

  model: ->
    App.Profile.fetch()

  afterModel: (profiles, transition) =>
    @transitionTo 'profile', profiles.get('firstObject')

}

此外,由於使用的是@ ,因此應使用粗箭頭=>正確引用this

希望能幫助到你。

我認為您覆蓋適配器的方式是錯誤的:

您正在使用jquery ajax推遲。 但是我認為您需要將該調用包裝在RSVP中 因為ember使用RSVP處理許可。

看看該實現

App.Profile.adapter = Ember.Adapter.create({
    ajaxSettings: function(url, method) {
        return {
            headers: {
                'Content-Type': 'application/json'
                'Authentication-Token': $.cookie('token')
            },
            dataType: "json"
        };
    },
    findAll: function(klass, records) {
        var url = '/api/users/' + $.cookie('user_id'),
        self = this;

        return this.ajax(url).then(function(data) {
            self.didFindAll(klass, records, data);
            return records;
        });
    }   
});

我使用了RESTAdapter中findAll實現,只是更改了URL,以匹配您想要的內容。

但是,如果您的響應僅返回一個對象,則認為find方法更好:

...
find: function(record, id) {
    var url = '/api/users/' + $.cookie('user_id'),
        self = this;

    return this.ajax(url).then(function(data) {
      self.didFind(record, id, data);
      return record;
    });
},
...

我終於通過Erik Bryn的jsFiddle找到了解決問題的方法。

我在這里錯過了return

App.Profile.adapter = Ember.Adapter.create {

  findAll: (klass, records) ->
    $.ajax('/api/users/' + $.cookie('user_id'), {
      type: 'get'
      headers: {
        'Content-Type': 'application/json'
        'Authentication-Token': $.cookie('token')
      }
      dataType: 'json'
    }).then (res) ->
      profiles = []
      // some stuff done here
      records.load klass, profiles
      return records // HERE

}

另外,我現在在ProfilesIndexRoute使用init方法,而不是redirect

App.ProfilesIndexRoute = Ember.Route.extend {

  init: ->
    App.Profile.fetch().then (profiles) =>
      @transitionTo 'profile', profiles.get('firstObject')

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM