dimanche 20 novembre 2016

Remove or refactor Ember observers manipulating attributes

Currently, I use a ton of observers throughout an application, and after watching a talk given by Stefan Penner on observers I've been trying to refactor observers to either computed properties or using component lifecycle hooks, but not really succeeding. Furthermore, I've been trying to apply the DDAU principle (Data Down Actions Up).

But how do you factor out observers on a component whose job it is to say for example go over an array (attribute given to the component) and manipulate it when a different attribute changes.

eg.:

// controller.hbs


// component.js
Ember.Component.extend({
 itemsObserver: function () {
  // some logic here that either adds or removes items from
  // for example object.selectedItems (an array) depending on
  // 'items'
 }.observes('items.[]')
})

I can't use 'didReceiveAttrs' because it doesn't work when an array is being manipulated (which is what I want to observe). And I wouldn't know how I'd use a computed property here when I don't really need a new property, I just need to manipulate an array on a given object (attribute to the component). But then again, I feel like that's completely contrary to the DDAU principle.

Could anyone enlighten me as so how I need to go about stuff in this situation, especially situations where an observer is used to manipulate another attribute, which in turn is being observed on a different component (it's a mess...).




Aucun commentaire:

Enregistrer un commentaire