我创建了一个js库(MessageBus.js)并使其与requirejs兼容.现在我想使用不带requirejs的同一个lib,即创建对象(新的MessageBus()).
我正在用这篇文章附上我的lib.
define([], function () {
var MessageBus = function () {
this.channelCallBackMap = {};
this.alreadyRegistred = false;
}
MessageBus.prototype = {
publish: function (channel, message) {
//Put original message and channel in the envelope and send it
var envelope = {
channel: channel,
message: message
};
var domain = location.protocol + '//' + location.host;
//Send message to all sibling iframes in the parent document
$("iframe", parent.document.body).each(function (i, frame) {
frame.contentWindow.postMessage(JSON.stringify(envelope), domain);
});
},
subscribe: function (channels, callbacks) {
var …Run Code Online (Sandbox Code Playgroud)