我创建了一个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 self = this;
if ($.isArray(channels) && $.isArray(callbacks)) {
$.each(channels, function (i, channel) {
self.channelCallBackMap[channel] = callbacks[i];
});
}
else if ($.isArray(channels)) {
$.each(channels, function (i, channel) {
self.channelCallBackMap[channel] = callbacks;
});
} else if (!$.isArray(callbacks)) {
this.channelCallBackMap[channels] = callbacks;
}
if (!this.alreadyRegistred) {
$(window).on('message', function (event) {
//Get the envelope, and from it get the original message as well as the channel
var domain = location.protocol + '//' + location.host;
if (event.originalEvent.origin !== domain) {
return;
}
var envelope = $.parseJSON(event.originalEvent.data);
if ($.inArray(envelope.channel, self.channels()) > -1) {
//Now it calls call-back function
self.channelCallBackMap[envelope.channel](envelope.channel, envelope.message);
}
});
}
this.alreadyRegistred = true;
},
channels: function () {
var keys = $.map(this.channelCallBackMap, function (value, key) {
return key;
});
return keys;
}
}
return MessageBus;
});
答案 0 :(得分:6)
尝试这样的事情:
!function (name, definition) {
if (typeof define == 'function' && define.amd) {
define(definition);
} else if (typeof module != 'undefined') {
module.exports = definition();
} else {
this[name] = definition();
}
}('MessageBus', function() {
var MessageBus = function () {
this.channelCallBackMap = {};
this.alreadyRegistred = false;
};
// Rest of the object
return MessageBus;
});
这是一种常见的语法,因为它也支持CommonJS。请参阅此库中的示例 - https://github.com/ded/klass/blob/master/klass.js
答案 1 :(得分:0)
当你不想作为一个人使用时,不确定为什么你把它写成AMD模块。
这根本不是一个好主意,但您可以在MessageBus.js
之前添加自己的方法,以便在全局范围内设置MessageBus
:
function define(dep, func)(
window.MessageBus = func()
)
更好的方法是拥有两个不同的版本。您可以拥有正常的JS版本,并使用grunt-combine任务构建另一个版本的任务
combine: {
amd: {
input: "amdContainer.js",
output: "MessageBus-amd.js",
tokens: [
{
token: "%MessageBus%",
file: "MessageBus.js"
}
]
}
<强> amdContainer.js 强>
define([], function () {
%MessageBus%
return MessageBus;
});
<强> MessageBus.js 强>
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 self = this;
if ($.isArray(channels) && $.isArray(callbacks)) {
$.each(channels, function (i, channel) {
self.channelCallBackMap[channel] = callbacks[i];
});
}
else if ($.isArray(channels)) {
$.each(channels, function (i, channel) {
self.channelCallBackMap[channel] = callbacks;
});
} else if (!$.isArray(callbacks)) {
this.channelCallBackMap[channels] = callbacks;
}
if (!this.alreadyRegistred) {
$(window).on('message', function (event) {
//Get the envelope, and from it get the original message as well as the channel
var domain = location.protocol + '//' + location.host;
if (event.originalEvent.origin !== domain) {
return;
}
var envelope = $.parseJSON(event.originalEvent.data);
if ($.inArray(envelope.channel, self.channels()) > -1) {
//Now it calls call-back function
self.channelCallBackMap[envelope.channel](envelope.channel, envelope.message);
}
});
}
this.alreadyRegistred = true;
},
channels: function () {
var keys = $.map(this.channelCallBackMap, function (value, key) {
return key;
});
return keys;
}
}