I want to open the Javascript API as a separate library without polluting their global namespace. I created a shell, so I do not pollute their requireJS according to http://requirejs.org/docs/faq-advanced.html . I have simplified what I still have, but I'm not sure if this is right, or if I have to do it differently.
var MyApi = MyApi || {}; var MyApiRequireJS = (function() { // require.js pasted here return {requirejs: requirejs, require: require, define: define}; })(); (function(require, define, requirejs) { require.config({ baseUrl: 'js/scripts', waitSeconds: 30, }); define( 'myapi', ['jquery', 'underscore'], function($, _) { $.noConflict(true); _.noConflict(); function api(method, args, callback) { // do stuff here } return {api: api}; } ); require( ['myapi'], function( myapi ) { MyApi = myapi; }); }(MyApiRequireJS.require, MyApiRequireJS.define, MyApiRequireJS.requirejs));
Sites using this library will include a script tag that links to the above code, and then call api using
MyApi.api('some_remote_method', {foo: 'bar'}, function(result) {
javascript requirejs
Wing lian
source share