[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)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
随机推荐
- ViewPager循环广告位的实现
1.如何实现循环播放 2.如何实现自动循环 如何实现循环播放 现在网上实现循环播放都是在adapter的getCount()方法返回一个较大的值并且instantiateItem(ViewGroup ...
- 说说RMAN里的obsolete
RMAN> report obsolete; RMAN retention policy will be applied to the commandRMAN retention policy ...
- 3.AOP入门1
1.定义1.1基本概念2. 1.定义 1.1基本概念 AOP:aspect object programing面向切面编程 aop编程的要点在于关注点和切入点 关注点:指的是代码中的重复部分,每次实现 ...
- PHP PDO获取结果集
一.介绍PDO获取结果集,不得不介绍一下PDO是如果执行SQL语句,一般情况下分三种, 1.query()方法 query()方法通常用于返回执行查询后的结果集.语法是这样的:PDOStatement ...
- 何为 pimpl ?
前言 你是否总因头文件包含冲突而苦恼? 你是否因头文件包含错乱而苦恼? 你是否因封装暴露了数据而苦恼? 你是否因经常改动实现而导致重新编译而苦恼? 在这里, 这些问题都不是问题, 跟随作者, 揭秘pi ...
- jQuery 的ready事件和 JavaScript 的load事件对比
为了理解2个事件的异同,先了解一下HTML文档加载顺序 HTML DOM文档加载步骤 HTML DOM文档加载是按顺序执行的,这与浏览器的渲染方式有关,一般浏览器渲染操作的顺序大致按如下几个步骤 1, ...
- sql 汉字转首字母拼音
从网络上收刮了一些,以备后用 create function fun_getPY(@str nvarchar()) returns nvarchar() as begin declare @word ...
- 9个最新的手机/移动设备jQuery插件
随着互联网的流行,移动网站开始急速增加,在2014年手机网站将会出现很多,所以手机网站是必须要学会制作的.手机网站不像桌面平台一样制作,否则会影响显示效果,目前大部分手机网站使用响应式设计技术,而且也 ...
- win7 IIS7.0 【IIS 管理器无法验证此内置帐户是否有访问权】
异常信息: 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS 管理器无法验证此内置帐户是否有访问权.请确保应用程序池标识具有该物理路径的读取访问权.如果此服务器加入到域 ...
- angularJS快速入门
1.引入脚本文件 <link rel="stylesheet" href="http://lib.sinaapp.com/js/bootstrap/v3.0.0/c ...