Created
August 5, 2014 17:04
-
-
Save koganei/9295afa6dc3a2f75b41a to your computer and use it in GitHub Desktop.
AngularJS DB Interface Layer
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
/** | |
* 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