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. UITabBarController自定义一

    UITabBarController自定义一 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewContro ...

  2. 【转】Hibernate和ibatis的比较

    1. 简介 Hibernate是当前最流行的O/R mapping框架.它出身于sf.net,现在已经成为Jboss的一部分了.iBATIS是另外一种优秀的O/R mapping框架,现已改名叫myB ...

  3. Linq介绍

    什么是LINQ? LINQ全称Language Integrated Query,中文翻译"语言集成查询".在.NET框架中,大致使用三大组件实现这个封装,分别 LINQ to O ...

  4. 初涉JavaScript模式 (12) : 沙箱模式

    引子 上一篇说了模块模式,而对于其中的命名空间模式其实也是有着一些问题,比如每添加一个模块或则深入叠加都会导致长命名,并且对于多个库的不同版本同时运行,一不小心就会污染全局标识,而这两天也发现了JSe ...

  5. System.InvalidOperationException: 无法加载协定为“ServiceReference1.XXXXXXXXXXXXXXXX”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。

    <system.serviceModel> <bindings> <basicHttpBinding> <binding name="testWeb ...

  6. jQuery中$.get()、$.post()和$.ajax()

    jQuery.get()方法: $.get(url,data,success(response,status,xhr),dataType) 该函数是简写的 Ajax 函数,等价于: $.ajax({ ...

  7. jquery 与其他库冲突解决方案

    var j = jQuery.noConflict(); j("div p").hide(); // 基于 jQuery 的代码 $("content").st ...

  8. day05

    1.递归 利用递归实现阶乘(1*2*3*4*5*6*7) def func(num): if num == 1: return 1 return num * func(num-1) x = func( ...

  9. SqlServer 还原,备份 Sql脚本命令

    备份 USE master go ),); set @path='d:\backup\'; set @name=@path+'TsDb.bak'; Backup database TsDb To di ...

  10. Sqlserver 正则替换函数的一种实现

    --函数 IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL DROP FUNCTION dbo.RegexReplace GO CREATE FUNCTION ...