javascript - How to inject an entire module dynamically in AngularJS? -
alright, i'm trying set-up $httpbackend use mock server local development against api. have 2 services in separate modules: search , searchmock:
- search returns $resource object exposes api verbs when in staging or production environments, , works expected
- searchmock exposes $httpbackend which, in turn, responds mock json objects, , works expected itself
i have service, apiinjector, determines current environment based on config file that's included dynamically grunt when app built , injects either search or searchmock accordingly.
my problem is, far can tell searching high , low, $httpbackend needs set-up within module's run method. problem can't inject run method within apiinjector's conditional logic.
how can expose $httpbackend if dev environment condition met, , $resource service otherwise? note didn't include calling controller's or searchservice's code, can if needed clarification.
searchmock:
var searchmockservice = angular.module('searchmockservice', []); searchmockservice.run([ '$httpbackend', function($httpbackend) { results = [{name: 'john'}, {name: 'jane'}]; $httpbackend.whenget('/search').respond(results); }]);
apiinjector:
var apiinjectorservice = angular.module('apiinjectorservice', [ 'searchservice', 'searchmockservice' ]); apiinjectorservice.factory('apiinjector', [ '$injector', 'env_conf', function($injector, env_conf) { var isdevelopment = env_conf.is_development; // check see if we're in dev and, if so, inject local api services if (isdevelopment) { return { search: // not sure here expose searchmock's run method??? }; } else { return { search: $injector.get('search') // returns $resource search service, expected }; } }]);
just put final point on question, pulled out build script (grunt) , abandoned above approach completely. instead, i'm passing in environment want build (development, staging, production, etc) flag grunt , loading appropriate api service during build.
hope helps else trying implement backendless development!
Comments
Post a Comment