最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Promise based property Ember - Stack Overflow

programmeradmin2浏览0评论

I've got a controller that has a searchQuery and suggestions property. The suggestions e from an AJAX request. How can I make the suggestions property a promise in my Controller?

app/controllers/application.js

import Ember from 'ember';

const { puted, $ } = Ember;

export default Ember.Controller.extend({
  searchQuery: '',
  suggestions: puted('searchQuery', function() {
    return $.getJSON(`songs/search.json?q=${this.get('searchQuery')}`);
  })
});

I've got a controller that has a searchQuery and suggestions property. The suggestions e from an AJAX request. How can I make the suggestions property a promise in my Controller?

app/controllers/application.js

import Ember from 'ember';

const { puted, $ } = Ember;

export default Ember.Controller.extend({
  searchQuery: '',
  suggestions: puted('searchQuery', function() {
    return $.getJSON(`songs/search.json?q=${this.get('searchQuery')}`);
  })
});
Share Improve this question edited Aug 16, 2015 at 20:23 Jon Koops asked Nov 15, 2013 at 19:58 Jon KoopsJon Koops 9,3016 gold badges32 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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

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.

发布评论

评论列表(0)

  1. 暂无评论