Promise within computed property


#1

I am trying to use a promise inside of a computed property. I need that because I want dynamic suggestions to be rendered when several selections are made by the user. In order to make handlebars be able to handle it I wrapped a DS.PromiseArray around the promise.

tag_suggestions: function() {
	var tag_ids = [];
	var chosenTags = this.get('chosenTags');

	chosenTags.forEach(function(chosenTag) {
		tag_ids.push(chosenTag.get('id'));
	});

	var component = this;

	// all of this returns a promise, but Handlebars can't handle the promise
	var promise = this.get('store').query('smartgroup/tag_suggestion', {'tag_ids': tag_ids}).then(function(tagSuggestions) {
		tagSuggestions = tagSuggestions.toArray();
		tagSuggestions = tagSuggestions.sortBy('count', 'desc').reverse();

		return tagSuggestions.slice(0, 5);
	});

	// that's why I need to create a promise array
   return DS.PromiseArray.create({
      promise: promise
   });

}.property('chosenTags'),

This made everything work. The problem is: I work with the rendered object and eventually send it back to the backend together with other rendered objects. What Ember tells me when I try to do is:

Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<filehero-frontend@model:tag::ember686:42>,<filehero-frontend@model:tag::ember688:121>,<(subclass of Ember.ObjectProxy):ember779>]

The first two objects have the correct type. The one that results of the resolved promise is of a subclass of Ember.ObjectProxy. Any ideas how to solve this problem?


#2

Well, the problem was not the entity “tag_suggestion” but the relation to another entity which was not resolved. I solved the problem by fetching the dependency from the store. If there is any better way let me know :slight_smile: