dustindiaz&ŧ039;的Publisher类实现了javascript中的Publisher-subscriber模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dustindiaz&ŧ039;的Publisher类实现了javascript中的Publisher-subscriber模式相关的知识,希望对你有一定的参考价值。
/* * Publisher Host (c) Creative Commons 2006 * http://creativecommons.org/licenses/by-sa/2.5/ * Author: Dustin Diaz | http://www.dustindiaz.com * Reference: http://www.dustindiaz.com/basement/publisher.html */ /* * @Class: Publisher * Requires: * Array.prototype.forEach * Array.prototype.some * See Sugar Arrays at: * http://www.dustindiaz.com/basement/sugar-arrays.html * Constructor: The almighty holder of its subscribers */ /* Minified */ function Publisher(){this.subscribers=[];}Publisher.prototype.deliver=function(data,thisObj){var scope=thisObj||window;this.subscribers.forEach(function(el){el.call(scope,data);});};Function.prototype.subscribe=function(publisher){var that=this;var alreadyExists=publisher.subscribers.some(function(el){if(el===that){return;}});if(!alreadyExists){publisher.subscribers.push(this);}return this;};Function.prototype.unsubscribe=function(publisher){var that=this;publisher.subscribers=publisher.subscribers.filter(function(el){if(el!==that){return el;}});return this;}; /* Full */ function Publisher() { this.subscribers = []; } /* * @desc: deliver | the delivery method * @param: data | The data you want to send to your subscribers * @param: thisObj | The scope you want to execute your callbacks */ Publisher.prototype.deliver = function(data, thisObj) { var scope = thisObj || window; this.subscribers.forEach( function(el) { el.call(scope, data); } ); }; /* * @desc: subscribe | Gives all function objects * the ability to subscribe to a Publisher Object * @param: publisher | The Publisher Object you wish to subscribe to */ Function.prototype.subscribe = function(publisher) { var that = this; var alreadyExists = publisher.subscribers.some( function(el) { if ( el === that ) { return; } } ); if ( !alreadyExists ) { publisher.subscribers.push(this); } return this; }; /* * @desc: unsubscribe | Gives all function objects * the ability to "unsubscribe" to a Publisher Object * @param: publisher | The Publisher Object you wish to unsubscribe to */ Function.prototype.unsubscribe = function(publisher) { var that = this; publisher.subscribers = publisher.subscribers.filter( function(el) { if ( el !== that ) { return el; } } ); return this; };
以上是关于dustindiaz&ŧ039;的Publisher类实现了javascript中的Publisher-subscriber模式的主要内容,如果未能解决你的问题,请参考以下文章
检查是否安装了Apache&ŧ039;的modŧu rewrite。