KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to expose a Javascript API as a standalone library without polluting their global namespace. I've created the wrapper so I don't pollute their own requireJS according to http://requirejs.org/docs/faq-advanced.html . I've simplified what I have so far as below, but I'm not sure if this is the correct way or if I should be doing it some other way. 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 would include a script tag referencing the above code and then call the api using MyApi.api('some_remote_method', {foo: 'bar'}, function(result) { // handle the result });
Tags (comma-separated)
Save Edits
Cancel