[AngularJS] Design pattern:simple Mediator

Source: Internet
Author: User

We ' re going to use Rootscope emit this send out events and then we ' re going to listen for them in the run block. We ' re going to use Rootscope on and the run block to listen for the same event, the we sent out to the system.

angular. Module (' App ', []). Controller (' Mainctrl ',function($scope, Order) {$scope. Neworder=function() {NewOrder ();}; }). Factory (' Order ',function($emit) {functionOrder () { This. email = ' [email protected] ';  This. Product = ' Macbook Pro '; $emit (' Order:created ', This); }        returnOrder; }). Factory (' $emit ',function($rootScope) {return function() {$rootScope. $emit. Apply ($rootScope, arguments);}; }). Factory (' Email ',function($window) {functionEmail (text) {$window. alert (text); }        returnEmail; }). Run (function($rootScope, Email) {$rootScope. $on (' Order:created ',function(event, order) {NewEmail (' Email sent to ' + Order.email + ' for ' +order.product);    }); });

We don ' t want ot inject $rootscope into Order factry, so we use mediator to create a $emit () function, and each time we CR Eate a new order, it'll send the Create event to run block. Isnide the run block we send the email. This is good because, the order doesn ' t know the Email event, it onlys emits a create event.

[AngularJS] Design pattern:simple Mediator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.