Created
May 3, 2013 15:12
-
-
Save SinisterMinister/5509725 to your computer and use it in GitHub Desktop.
API adapter prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Base API that all adapters will wrap | |
*/ | |
var BaseApi = (function ($){ | |
// Force strict | |
"use strict"; | |
/** | |
* Updates the output div | |
* @param {string} funcName Name of the function being called | |
* @return {void} | |
*/ | |
var updateOutput = function (funcName, version) { | |
$('#output').html('Just fired `'+funcName+'()` from version '+version); | |
}; | |
return { | |
updateOutput: updateOutput | |
}; | |
})(jQuery); | |
var Adapter = (function (BaseApi) { | |
// Force strict | |
"use strict"; | |
var adapters = {}, | |
baseApi = BaseApi, | |
adapterFactoryException = function (message) { | |
return { | |
name: 'AdapterFactoryException', | |
message: message | |
}; | |
}, | |
factory = function (version) { | |
if (typeof adapters[version] === 'undefined') | |
throw adapterFactoryException('Adapter version `'+version+'` does not exist!'); | |
if (typeof adapters[version].publicInterface === 'undefined') | |
throw adapterFactoryException('Adapter must have a publicInterface!'); | |
return adapters[version].publicInterface; | |
}, | |
register = function (adapter) { | |
if (typeof adapter.version === 'undefined') | |
throw adapterFactoryException('Adapter must have a version!'); | |
if (typeof adapters[adapter.version] !== 'undefined') | |
throw adapterFactoryException('An adapter of with this version is already registered!'); | |
adapters[adapter.version] = adapter; | |
}, | |
extend = function (version, adapter) { | |
if (typeof adapters[version] === 'undefined') | |
throw adapterFactoryException('Adapter version `'+version+'` does not exist!'); | |
return $.extend(true, {}, adapters[version], adapter); | |
}; | |
return { | |
factory: factory, | |
register: register, | |
extend: extend | |
}; | |
})(BaseApi); | |
(function (Adapter, api) { | |
// Force strict | |
"use strict"; | |
var version = '0.1', | |
adapter = { | |
version: version, | |
publicInterface: { | |
foo: function () { | |
api.updateOutput('foo', version); | |
} | |
} | |
}; | |
Adapter.register(adapter); | |
})(Adapter, BaseApi); | |
(function (Adapter, api) { | |
// Force strict | |
"use strict"; | |
var version = '0.2', | |
adapter = Adapter.extend('0.1', { | |
version: version, | |
publicInterface: { | |
bar: function () { | |
api.updateOutput('bar', version); | |
} | |
} | |
}); | |
Adapter.register(adapter); | |
})(Adapter, BaseApi); | |
(function (Adapter, api) { | |
// Force strict | |
"use strict"; | |
var version = '0.3', | |
adapter = Adapter.extend('0.2', { | |
version: version, | |
publicInterface: { | |
baz: function () { | |
api.updateOutput('baz', version); | |
}, | |
foo: function () { | |
api.updateOutput('foo', version); | |
} | |
} | |
}); | |
Adapter.register(adapter); | |
})(Adapter, BaseApi); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment