Can anyone suggest a template that can be used to write the JavaScript API where there is no common code between multiple implementations? The idea is to provide a customer consumer with one wrapping API for one of the many possible APIs defined at runtime. An API call can be associated with objects / libraries already in the application environment or web service calls.
The following bits of pseudo code are the two approaches that I have examined:
Monolithic solution
var apiWrapper = {
init: function() {
this.context = App.getContext();
},
getName: function() {
switch(context) {
case a:
return a.getDeviceName()
case b:
return b.deviceName
etc...
}
}
}
Pros . May support consistent API for library users.
Cons : will lead to the creation of a huge monolithic library that is difficult to maintain.
Solution for modules
init.js
require([App.getContext()], function(api) {
var apiWrapper = api;
});
module_a.js
define(function() {
var module = {
getName: function() {
return deviceA.getDeviceName();
}
};
return module;
});
module_b.js
define(function() {
var module = {
getName: function() {
return deviceB.getName;
}
};
return module;
});
: .
. , API . DRY.
, - , , Js.
?