简体   繁体   English

基于承诺的财产Ember

[英]Promise based property Ember

I've got a controller that has a searchQuery and suggestions property. 我有一个具有searchQuerysearchQuery属性的控制器。 The suggestions come from an AJAX request. 建议来自AJAX请求。 How can I make the suggestions property a promise in my Controller? 如何在控制器中将建议属性作为承诺?

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. 我假设你的意思是,我怎样才能从承诺中得到结果,因为你要向suggest属性返回一个承诺。

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

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. 模型钩子和transitionTo / transitionToRoute方法。 The rest of the time they leave it up to you, in case you actually wanted to keep track of the promise. 其余的时间他们会留给你,以防你真的想跟踪承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM