mardi 11 octobre 2016

Ember.js - Filtering all properties of a model

I am trying to filter all properties of a model by an input field in Ember.js. If the text field input is found in one of the models properties, I want to return the filtered results.

I already have a working solution, but i wanted to know if there is a better way to filter all properties, instead of getting them one after another. I already did a lot of research, but I could not find any better way to solve this problem. Here is the relevant code:

Model:

[
  {
    "source": "this is a source", 
    "title": "this is a title",
    "message":"this is a message"
  },
  {
    "source": "this is a source too", 
    "title": "this is a title too",
    "message":"this is a message too"
  }
]

Filter:

 export default Ember.Component.extend({

  filterText: "", //Input field value

  filteredArticles: Ember.computed('filterText', function () {
    var filter = this.get('filterText').toLowerCase();
    return this.get('model').filter(function (item) {
      return item.get('title').toLowerCase().indexOf(filter) !== -1 ||
        item.get('message').toLowerCase().indexOf(filter) !== -1 ||
        item.get('source').toLowerCase().indexOf(filter) !== -1
    });
  })
});

So is there a way to get all properties with one command (e.g. like item.get('allProperties'))?

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire