Skip to content

pussinboots/angularjs-interpolate-interceptor

Repository files navigation

Build Status Coverage Status Dependencies heroku

AngularJS Module to define dynamic variables in ngResource defintion like familiar with in templates. Its has the same bracket syntax for exampel {{config.value}} could also be used in the ngResource url definition and will be replaced with the correspondant value when the request is performed.

Dependencies

##Usage

  • download js file
  • added javascript file to your app html file
<script type='text/javascript' src="angularjs-interpolate-interceptor.js"></script>
  • add module to the app.js and register interpolateInterceptor as http interceptor
var appModule = angular.module('app', ['angularjs-interpolate-interceptor'])
appModule.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('interpolateInterceptor');
}])

That done. Now you can use the angularjs bracket syntax to define variables in the nrResource service url definition. For example

angular.module('Services', ['ngResource'], function ($provide) {
    $provide.factory('ServiceA', function ($resource, $rootScope) {
        var resource = $resource('/{{config.valuea}}/', {}, {
            get: {method: 'GET', params: {}},
        });
        return resource;
    });
});

The variable {{config.valuea}}

var resource = $resource('/{{config.valuea}}/', {}, {

Issues

Todos

  • at the moment the variable is hardcoded with name config and has to be registered on the $rootScope make it configurable
request: function (config) {
    var exp = $interpolate(config.url);
    config.url  = exp({config: $rootScope.config})
    return config;
}

Features

  • support variable in url of ngResource service definition

Configuration

missing

##Example

Simple Variable Example

Define on the $rootScope the config object amd use it on the service defintion like

appModule.run(function ($rootScope) {
	$rootScope.config = {valuea:'test', valueb:'prod'}
});

angular.module('Services', ['ngResource'], function ($provide) {
    $provide.factory('ServiceA', function ($resource, $rootScope) {
        var resource = $resource('/{{config.valuea}}/', {}, {
            get: {method: 'GET', params: {}},
        });
        return resource;
    });
});

Demo

live

Variable Example

local

Two ways to run the demo app local one with play or second with nodejs.

Dependencies

Start it with play

play run

Then go to

Start it with nodejs

node server.js

Then go to

Or run the karma test local with

npm test

Description

The motivation is to support dynamic variables for ngResource service defintion to change urls at runtime for example or to inject global variables. Maybe it could also be used to minimize the controller logic to only bind data for the view and the service defined which variable he need for his request. It is a young project and started as proof of concept maybe they can support other uise case. Feel free to try and develop it to support your needs. Questions, thanks, ideas contact me.

License

angularjs-interpolate-interceptor is released under the MIT License.

About

AngularJS modul to support variables in service defintition

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published