angularjs - Configure Restangular.baseUrl using Gruntjs -
i'm using restangular in project built using gruntjs. here snippet:
// scripts/app.js angular.module('myapp', ['restangular'])]) .config(['restangularprovider', function(restangularprovider) { /* different on dev, test, prod environments */ var baseurl = 'http://localhost:8080/sms-api/rest/management/'; restangularprovider.setbaseurl(baseurl); }])
i have different value baseurl
if specified @ cli or default if not specified:
$ grunt server using default value 'baseurl' $ grunt build --rest.baseurl='http://my.domain.com/rest/management/' using 'http://my.domain.com/rest/management/' 'baseurl'
how can that?
it's possible aid of grunt preprocess useful replacing (and other things) templates inside files.
first add .js code:
/* begin insertion of baseurl gruntjs */ /* @ifdef baseurl var baseurl = /* @echo baseurl */ // @echo ";" // @endif */ /* @ifndef baseurl var baseurl = 'http://www.fallback.url'; // @endif */ /* end of baseurl insertion */
then in grunt file, after installing grunt preprocess (i.e npm install grunt-preprocess --save-dev
add following configuration:
preprocess: { options: { context: { } }, js: { src: 'public/js/services.js', dest: 'services.js' } },
obviously, need update js file list accordingly ever files use. important notice - if planning on updating same file (and not new destination) need use inline option
at last, in order work command line config variable
, add following custom task grunt file well:
grunt.registertask('baseurl', function () { var target = grunt.option('rest.baseurl') || undefined; grunt.log.ok('baseurl set to', target); grunt.config('preprocess.options.context.baseurl', target); grunt.task.run('preprocess'); });
finally, run baseurl task so:
grunt baseurl --rest.baseurl='http://some.domain.net/public/whatever'
notice put in fallback url can run grunt baseurl
, grunt set baseurl defined one.
Comments
Post a Comment