2014-07-12 26 views
0

我的最終目標是在網站的主頁上設置一個文本字段,用於偵聽輸入內容,並且每次按鍵都會從Google的Places API抓取數據,列表到主頁。我目前正在嘗試爲此使用計算屬性,但似乎遇到綁定不等待API請求返回的問題。如何從Google API中獲取Ember中的自動填充字段的數據

我一直在閱讀承諾,但不知道如何將它們合併到我的控制器。

這是我目前的嘗試:

App.IndexController = Ember.Controller.extend 
    service: new google.maps.places.AutocompleteService() 
    searchText: '' 

    searchResults: (-> 
    # service = new google.maps.places.AutocompleteService() 
    @get('service').getPlacePredictions({input: @get('searchText')}, (predictions, status)-> 
     console.log(predictions) 
    ) 
).property('searchText') 

控制檯登錄正確的結果,但SearchResult所屬性不會得到更新。

我的問題是如何在API請求成功後獲得綁定更新,這是組織這種邏輯的最佳方式嗎?

感謝您的期待。

回答

1

我不確定Ember Data是否接受承諾作爲屬性值,但是您可以嘗試在RSVP承諾中包裝google.maps.places.AutocompleteService響應來測試該承諾。

這是另一個應該工作的解決方案。特別注意胖箭頭和@set

searchResults: (-> 
    @get('service').getPlacePredictions({input: @get('searchText')}, (predictions, status)=> 
    @set('searchResults', predictions) 
) 
).property('searchText') 
1

基於由Pooyan的提示,這是我結束了去:

App.IndexController = Ember.Controller.extend 
    service: new google.maps.places.AutocompleteService() 
    searchText: '' 
    results: null 

    searchTextChanged: (-> 
    if @get('searchText').length 
     @get('service').getPlacePredictions({input: @get('searchText')}, (predictions, status) => 
     console.log("update map here...") 
     @set('results', predictions) 
    ) 
    else 
     @set('results', null) 
).observes('searchText') 

基本上,我用了一個觀察者留意我的文本字段。當文本字段發生變化時,我們會撥打Google的服務來獲取結果。然後,我們將這些結果設置爲結果屬性。

+0

您是否嘗試從財產申報中返回承諾?如果它有效,這是一個更好的方法,如果沒有的話,我相信你可以發送PR到Ember Data倉庫,他們會接受。 –

+0

@PooyanKhosravi,我不知道這將如何設置完全。你能舉一個例子嗎? – jklina

相關問題