如何将 jquery 命名空间插件转换或放入 require 样板中?例如,
这通常是我的标准 jquery 样板,
// A namepace structure:
(function($){
// Initial setting.
var pluginName = 'BR_account';
var storageName = 'plugin_' + pluginName;
var methods = {
init : function( options ) {
console.log("this is a jquery plugin boilerplate in requirejs boilerplate"); // nothing returned.
}
};
$.fn[pluginName] = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments ); // always change 'init' to something else if you different method name.
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.' + pluginName + '.' );
}
return this;
};
$.fn[pluginName].defaults = {
onSuccess: function() {}
};
})(jQuery);
这通常是需要的样板,
//Filename: boilerplate.js
define([
// These are path alias that we configured in our bootstrap
'jquery', // lib/jquery/jquery
'underscore', // lib/underscore/underscore
'backbone' // lib/backbone/backbone
], function($, _, Backbone){
// Above we have passed in jQuery, Underscore and Backbone
// They will not be accessible in the global scope
return {};
// What we return here will be used by other modules
});
但我怎样才能将它们混合在一起——或者也许我不应该?
这是我的测试,
define([
'jquery',
'underscore',
'backbone'
], function($, _, Backbone){
console.log(Backbone); // I get an object of Backbone
// A namepace structure:
(function($){
// Initial setting.
var pluginName = 'BR_account';
var storageName = 'plugin_' + pluginName;
var methods = {
init : function( options ) {
console.log("this is a jquery plugin boilerplate in requirejs boilerplate"); // nothing returned.
}
};
$.fn[pluginName] = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments ); // always change 'init' to something else if you different method name.
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.' + pluginName + '.' );
}
return this;
};
$.fn[pluginName].defaults = {
onSuccess: function() {}
};
})(jQuery);
});
当然这是行不通的。有任何想法吗?