所以我一直在阅读 CommonJs Modules 规范并查看 dojo 实现和 google 闭包实现。这个概念很酷,但我遇到了使用 AMD 将您的应用程序紧密耦合的问题。
关闭站点的示例:
goog.provide('tutorial.notepad');
goog.provide('tutorial.notepad.Note');
goog.require('goog.dom');
goog.require('goog.ui.Zippy');
/**
* Iterates over a list of note data objects, creates a Note instance
* for each one, and tells the instance to build its DOM structure.
*/
tutorial.notepad.makeNotes = function(data, noteContainer) {
var notes = [];
for (var i = 0; i < data.length; i++) {
var note =
new tutorial.notepad.Note(data[i].title, data[i].content, noteContainer);
notes.push(note);
note.makeNoteDom();
}
return notes;
};
/**
* Manages the data and interface for a single note.
*/
tutorial.notepad.Note = function(title, content, noteContainer) {
this.title = title;
this.content = content;
this.parent = noteContainer;
};
/**
* Creates the DOM structure for the note and adds it to the document.
*/
tutorial.notepad.Note.prototype.makeNoteDom = function() {
// Create DOM structure to represent the note.
this.headerElement = goog.dom.createDom('div',
{'style': 'background-color:#EEE'}, this.title);
this.contentElement = goog.dom.createDom('div', null, this.content);
var newNote = goog.dom.createDom('div', null,
this.headerElement, this.contentElement);
// Add the note's DOM structure to the document.
goog.dom.appendChild(this.parent, newNote);
return new goog.ui.Zippy(this.headerElement, this.contentElement);
};
所以我的问题是这里没有紧密耦合吗?如果您向应用程序提供 tutorial.notepad 并且某些其他模块需要它,并且 tutorial.notepad 中的功能更改在这里不存在紧密耦合问题。基本上,您将应该能够独立存在的模块链接在一起,从而创建了一个脆弱的架构。
如果有人可以在值得赞赏的架构环境或任何有关构建松散耦合 AMD 架构的资源中谈论这个问题,我可能会想到这个错误。