Skip to content

Instantly share code, notes, and snippets.

@koganei
Created August 5, 2014 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koganei/9295afa6dc3a2f75b41a to your computer and use it in GitHub Desktop.
Save koganei/9295afa6dc3a2f75b41a to your computer and use it in GitHub Desktop.
AngularJS DB Interface Layer
/**
* The idea is to expose an interface from the app side that a service can fulfill
*/
angular.module('myapp', ['apiservice'])
/**
* We load the service that fulfills the interface through a config on the app side
*/
.factory('config', function(
// poemsInterface
poemsApi
) {}).run(function(config) {})
/**
* The interface to fulfill
*/
.factory('poemsInterface', function() {
return {
getPoem: function() {},
savePoem: function() {},
};
})
/**
* We make use of the interface, could be in a controller or wherever
*/
.run(function($rootScope, poemsInterface) {
$rootScope.foo = poemsInterface.getPoem();
});
/**
* The service that will implement the interface
*/
angular.module('apiservice', [])
.factory('poemsApi', function(poemsInterface) {
var poemsApi = {
getPoem: function() { return 'my poem from db'; }
};
poemsInterface = angular.extend(poemsInterface, poemsApi);
return poemsInterface;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment