We're going to use rootScope emit here to send out events and then we're going to listen for them in the run block. We're going to use rootScope on down in the run block to listen for the same event that we sent out to the system.

angular
.module('app', [])
.controller('MainCtrl', function($scope, Order) {
$scope.newOrder = function() { new Order(); };
})
.factory('Order', function($emit) {
function Order() {
this.email = 'brett.shollenberger@gmail.com';
this.product = 'Macbook Pro';
$emit('order:created', this);
}
return Order;
})
.factory('$emit', function($rootScope) {
return function() { $rootScope.$emit.apply($rootScope, arguments); };
})
.factory('Email', function($window) {
function Email(text) {
$window.alert(text);
}
return Email;
})
.run(function($rootScope, Email) {
$rootScope.$on('order:created', function(event, order) {
new Email('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 create a new order, it will 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的更多相关文章

  1. 简单工厂设计模式(Simple Factory Design Pattern)

    [引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...

  2. Design Pattern in Simple Examples

    Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...

  3. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  4. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  5. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  6. C++ Design Pattern: What is a Design Pattern?

    Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when deve ...

  7. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  8. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  9. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

随机推荐

  1. Sql Server跨服务器导出特定表数据

    --连接服务器 exec sp_addlinkedserver 'Test',' ','SQLOLEDB', '192.168.0.7'; exec sp_addlinkedsrvlogin 'Tes ...

  2. [转] 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...

  3. RB1001: IE6 IE7 IE8(Q) 负边距 (margin) 导致元素溢出 hasLayout 容器时显示异常

    标准参考 根据W3C CSS2.1规范第8.3节中的描述,边距属性设置了一个框的边距区的宽度.'margin' 缩写属性设置所有四边的边距,而其它的边距属性( 'margin-top' ,'margi ...

  4. git操作(强烈推荐)

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  5. ext之关键字mixins、statics、require

    1.mixins 说明:类似于面向对象中的多继承 <script type="text/javascript"> Ext.onReady(function () { / ...

  6. sublime3 插件pylinter的安装

    1.首先sublime需要安装package control,之后安装pylinter插件,并进行简单的属性配置(网上教程很多,略) 2.之后是pylint_path的配置,我参照网上的配置失败,su ...

  7. 好用的JQ图片特效jquery-poptrox-popup-galleries

    jQuery Poptrox – Popup galleries     Rate this (1 Vote) Download   Demo jQuery Poptrox Adds popup ga ...

  8. file upload download

    1. 文件上传与下载 1.1 文件上传 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 文件上传,要点: 前台: ...

  9. javasript生成 uuid的几种算法分享

    方式一 function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i &l ...

  10. 浅谈Java内存泄露

    一.引言 先等等吧……累了