samedi 26 septembre 2015

Filtering Emberjs Data by Attribute Properties

So I have an Ember-cli 1.13.8 application where I would like to view the questions without answers. Currently, I can filter searches based on attributes. For example, I can search for the "content" of questions because that is an attribute on the object.

Models:

answer:

export default DS.Model.extend({
  ...
  content: DS.attr(),
  question: DS.belongsTo('question', { async: true })
});

questions:

export default DS.Model.extend({
  ...
  answers: DS.hasMany('answer', { async: true }),
});

For example, this works to query attributes of the current model (question):

model(query) {
 return this.store.findAll('question').then(function(questions) {
   return questions.filterBy("content", query);
 });
}

But I cannot filter based upon the properties (in this case length) of the attributes of the model. I would like to filter based upon those questions which have no answers.

model() {
  return this.store.findAll('question').then(function(questions) {
    return questions.filter(function(item, index, enumberable) {
      return item.answers.length === 0;
    })
  });
}

I have consulted: Emberjs filter() versus filterProperty()

It seems that filterProperty() has been deprecated so examples such as: http://ift.tt/1MwACto are not helpful.




Aucun commentaire:

Enregistrer un commentaire