christianalfoni / flux-angular

Use the FLUX architecture with Angular JS
313 stars 50 forks source link

Emit custom event from each store #59

Closed creative-eye closed 8 years ago

creative-eye commented 8 years ago

We could emit specific events on each store, instead of the standard this.emitChange(). This might be useful if we have multiple things updating at once, but are interested only in a specific change. What do you think?

jrust commented 8 years ago

This is fixed in 3.0.0 since you can now listen for changes on a specific leaf of your data tree. e.g.: a store that has the following state:

initialize: function() {
  this.state = this.immutable({ selectedColor: 'blue', colors: ['green', 'blue'] });
}

And in the controller:

// Listen only for changes to selectedColor
$scope.$listenTo(colorStore, ['selectedColor'], yourCallback);

Alternatively, you could also look at the paths changed since that is passed to callbacks that listen to all changes on a store:

$scope.$listenTo(colorStore, function(e) {
  if (_.includes(e.data.paths, ['selectedColor'])) {
    // selectedColor has changed
  }
});

See the baobab docs on events for more details.