mardi 8 décembre 2015

Removing an itemController when upgrading to Ember 2.0

I am re-writting my app to be compliant with Ember 2 and have run into a big problem when trying to refactor an ItemController.

Currently I have an arrayController that has an array of items. The user has a number of options for sorting the data that is displayed that can be selected. Some of the sorts depend directly on a property set on the controller (supplied by the user via the template). To do this I currently declare an item controller, which means that I can create a computed property to compute the sort value I need. This can’t be done via a computedProperty on the model as the model does not know the controller property that has been set.

How do I refactor this to not use itemControllers - I’m at a loss.

I've put some code snippets together to try to explain what I mean. I am trying to sort on the itemController property numberOfSelectedFruit the value of which for each 'person' will depend on the selectedFruit property on the controller (Picked up from the template). It's a silly example but I think it illustrates the issue.

I really appreciate any help here on what is the ember 2 way of doing this (I can't see how components can help either)

// model
APP.Person = DS.Model.extend({
  name: DS.attr('string'),
  // Not proper code, but to show the structure of the fruit field
  fruit: [{type: DS.attr(‘string’), number: DS.attr(‘number’}]
})

// person_controller
APP.PersonItemController = Ember.ObjectController.extend({
  numberOfSelectedFruit: function() {
    var parentController = this.get('parentController');

    var fruitType = parentController.selectedFruit;
    var numberOfFruit = 0;
    this.get(‘fruit’).forEach(function(aFruit) {
      if(aFruit.type === fruitType) {
        numberOfFruit++;
      }
    });
    return numberOfFruit;
  }
})

APP.PersonController = Ember.ArrayController.extend({
  itemController: 'PersonItem',
  sortingOn: 'name',
  selectedFruit: 'orange',
  sortedPeople: Ember.computed.sort('filteredContent', 'sortingOn'),

})




Aucun commentaire:

Enregistrer un commentaire