[AngularJS] Design Pattern: Simple Mediator
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的更多相关文章
- 简单工厂设计模式(Simple Factory Design Pattern)
[引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...
- 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 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- 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 ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
随机推荐
- for update被锁定解锁
查找被锁定的表,用户,session:SELECT object_name, machine, s.sid, s.serial#FROM gv$locked_object l, dba_object ...
- 【转】Auto Layout 进阶
原文:http://blog.csdn.net/ysy441088327/article/details/12558097 引言: Auto Layout是iOS6发布后引入的一个全新的布局特性, ...
- Linux2.6的所有内核版本
Index of /pub/linux/kernel/v2.6 Name Last modified Size Parent Directory - incr/ 03-Aug-2011 20:47 - ...
- c#求slope线性回归斜率
public class mySlope { // public List<double> Values { get; set; } public double SlopeResult { ...
- 【USACO 2.3.2】奶牛家谱
[题目描述] 农民约翰准备购买一群新奶牛.在这个新的奶牛群中,每一个母亲奶牛都生两小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些二叉 ...
- NoSQL数据库技术特性解析之文档数据库
现今云计算的从业人员对NoSQL一词并不感到陌生,虽然很多技术人员都长期从事关系数据库的工作,但现在他们对NoSQL技术充满期待.对于企业来说,从关系型数据库到NoSQL数据库转变绝对是个需要深思熟虑 ...
- Qt中绘图坐标QPainter,Viewport与Window的关系
在Qt中常常要自己重载一些paintEvent函数,这个时候往往忽略了两个很关键的API,那就是setViewport和setWindow. Viewport,顾名思义,反应的是物理坐标,就是你实际想 ...
- terminal命令
新建一个文件并写入内容: echo hello world > a.txt (每次echo都会重写文件) 新建文件: touch a.txt 新建目录: mkdir work 用vim打开文件 ...
- android异常之emulator-arm.exe已停止工作
我遇到的这个问题通过降低了AVD的分辨率后解决了,估计是电脑的显卡不行.
- Day4 内置函数补充、装饰器
li = [11,22,33,44]def f1(arg): arg.append(55)#函数默认返回值None,函数参数传递的是引用li = f1(li) print(li) 内置函数补充: ...