2013-08-21 58 views
0

我正在嘗試使用Ember模型做一些基本的工作,但我遇到了一些帶有承諾的奇怪行爲。不知道我很清楚它應該如何工作。當他們訪問/profilesEmber Model問題承諾

App.ProfilesIndexRoute = Ember.Route.extend { 

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

} 

我只是希望人們被重定向到/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 

我在做什麼錯在這裏?有人here建議我使用fetch而不是find,但它似乎沒有解決我的問題,因爲事情沒有以正確的順序執行。

謝謝!因爲使用的是@你應該使用脂肪箭頭=>有正確的參考this

App.ProfilesIndexRoute = Ember.Route.extend { 

    model: -> 
    App.Profile.fetch() 

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

} 

此外:

回答

1

我終於找到了如何將問題解決感謝對this jsFiddle by Erik Bryn

我缺少一個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,而不是redirect使用init方法:

App.ProfilesIndexRoute = Ember.Route.extend { 

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

} 
1

你應該重定向到從afterModel鉤不同的路線。

希望它有幫助。

1

我認爲你overring適配器的方式是錯誤的:

您正在使用遞延jquery的AJAX。但我認爲你需要wrap that call in RSVP。因爲燼使用RSVP處理promisses。

給在執行

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; 
     }); 
    } 
}); 

我用findAll實現從RESTAdapter,只是改變了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; 
    }); 
}, 
...