#AngularJS: $broadcast and $on events #JS
// if you need to broadcast any changes that has been done to other components of the app
// then use $broadcost to create event and then in the 'listening' place include $on
var module = angular.module( "my.new.module", [] );
module.service( 'Book', [ '$rootScope', function( $rootScope ) {
var service = {
books: [
{ title: "Magician", author: "Raymond E. Feist" },
{ title: "The Hobbit", author: "J.R.R Tolkien" }
],
addBook: function ( book ) {
service.books.push( book );
$rootScope.$broadcast( 'books.update' );
}
}
return service;
}]);
// via http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/
var ctrl = [ '$scope', 'Book', function( scope, Book ) {
scope.$on( 'books.update', function( event ) {
scope.books = Book.books;
});
scope.books = Book.books;
}];
module.controller( "books.list", ctrl );