4

I'm looking through angularjs examples, I've found this example:

// This is a module for cloud persistance in mongolab - https://mongolab.com
angular.module('mongolab', ['ngResource']).
factory('Project', function($resource) {
  var Project = $resource('https://api.mongolab.com/api/1/databases' +
      '/angularjs/collections/projects/:id',
      { apiKey: '4f847ad3e4b08a2eed5f3b54' }, {
        update: { method: 'PUT' }
      }
  );

  Project.prototype.update = function(cb) {
    return Project.update({id: this._id.$oid},
        angular.extend({}, this, {_id:undefined}), cb);
  };

  Project.prototype.destroy = function(cb) {
    return Project.remove({id: this._id.$oid}, cb);
  };

  return Project;
});

I don't want using magic string static resource such as https://api.mongolab.com/api/1/databases/angularjs/collections/projects/:id, instead I would like to have it defined on server and later passed into the module. My question is, how do you parametrize module, i.e. how do you pass a javascript variable into the module from outside?

3
  • Maybe this one could inspire you doing it stackoverflow.com/questions/16339595/… Commented May 4, 2013 at 20:54
  • I'll try defining my config module in View file, thanks Commented May 4, 2013 at 21:14
  • I don't know if I understand correctly what you are looking for, but I think you need to use provider instead of factory docs.angularjs.org/guide/providers#provider-recipe Commented Jun 11, 2015 at 14:47

1 Answer 1

0

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

var app = angular.module('mongolab', ['ngResource'])
app.provider('project', function projectProvider(){
    var resourceUrl = false;

    this.resourceUrl = function(url){
        this.resourceUrl = url;
    }

    this.$get = [function project(){
        return new Project(resourceUrl);
    }];
});

function Project(resourceUrl) {
  var Project = $resource(resourceUrl,
      { apiKey: '4f847ad3e4b08a2eed5f3b54' }, {
        update: { method: 'PUT' }
      }
  );

  Project.prototype.update = function(cb) {
    return Project.update({id: this._id.$oid},
        angular.extend({}, this, {_id:undefined}), cb);
  };

  Project.prototype.destroy = function(cb) {
    return Project.remove({id: this._id.$oid}, cb);
  };

  return Project;
});

Then you can config it with

app.config(["projectProvider", function(projectProvider){
    projectProvider.resourceUrl('https://api.mongolab.com/api/1/databases' +
        '/angularjs/collections/projects/:id')
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.