基于承诺的物业Ember [英] Promise based property Ember
问题描述
我有一个控制器,它有一个 searchQuery
和建议属性。建议来自AJAX请求。我的控制器如何使建议属性成为承诺?
I've got a controller that has a searchQuery
and suggestions property. The suggestions come from an AJAX request. How can I make the suggestions property a promise in my Controller?
app / controllers / application.js
p>
app/controllers/application.js
import Ember from 'ember';
const { computed, $ } = Ember;
export default Ember.Controller.extend({
searchQuery: '',
suggestions: computed('searchQuery', function() {
return $.getJSON(`songs/search.json?q=${this.get('searchQuery')}`);
})
});
推荐答案
我假设你的意思是,如何获得结果从承诺开始,因为您向建议财产返还承诺。
I assume you mean, how can I get the results from the promise, since you are returning a promise to the suggestions property.
searchQuery: '',
suggestions: [],
suggestionsUpdater: Ember.observer('searchQuery', function(){
var self = this;
Ember.$.getJSON('songs/search.json?q=' + this.get('searchQuery')).then(function(data){
self.set('suggestions', data);
});
})
只有几个地方可以返回/发送承诺,假设你不想存储承诺。模型钩子,和transitionTo / transitionToRoute方法。其余的时间,他们把它留给你,以防你真的想跟踪承诺。
There are only a few places where you can return/send a promise and ember's going to assume you didn't want to store the promise. The model hook, and transitionTo/transitionToRoute methods. The rest of the time they leave it up to you, in case you actually wanted to keep track of the promise.
这篇关于基于承诺的物业Ember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!