我正在将JSF重型web应用程序迁移到REST,主要是JS模块应用程序。
我看过Nicholas Zakas在YUI剧院(优秀视频)上的“可伸缩的javascript应用程序体系结构”,并成功地实现了其中的大部分内容,但我有一些问题:
dojo.require之类的东西,因此实际上我隐藏了基本库,但是模块本身是由基库定义和加载的.我觉得有点奇怪。--$$('.classname').each(..之类的操作,这可以大量地清除代码,封装基本库是没有意义的,然后在模块中通过执行.each为基本库创建一个依赖项,但不使用这些特性会使编写的大量代码被忽略……实现该功能非常容易出错。进行表单验证)有多容易?
。
发布于 2011-04-24 04:00:10
我们在应用程序中大量使用这种模式。有关如何实现Sandbox模式的详细内容,请参阅Stoyan Stoyan的“JavaScript Patterns”一书。基本上看起来是这样的:
(function (global) {
var Sandbox = function fn (modules, callback) {
var installedModules = Sandbox.modules,
i = 0,
len = modules.length;
if (!(this instanceof fn)) {
return new fn(modules, callback);
}
// modules is an array in this instance:
for (; i < len; i++) {
installedModules[modules[i]](this);
}
callback(this);
};
Sandbox.modules = {};
global.Sandbox = Sandbox;
})(this);
// Example module:
// You extend the current sandbox instance with new functions
Sandbox.modules.ajax = function(sandbox) {
sandbox.ajax = $.ajax;
sandbox.json = $.getJSON;
};
// Example of running your code in the sandbox on some page:
Sandbox(['ajax'], function(sandbox) {
sandbox.ajax({
type: 'post',
url: '/Sample/Url',
success: function(response) {
// success code here. remember this ajax maps back to $.ajax
}
});
});https://stackoverflow.com/questions/3628649
复制相似问题