Skip to content Skip to sidebar Skip to footer

Default $resource Post Data

That might be strange but I need to specify some default POST data for my $resource using the factory method of the module. Does anyone have an idea of how to do that in AngularJS

Solution 1:

This is not really the angular way to do such a thing as you lose data consistency if you do it and it doesn't reflect in your model.

Why?

The resource factory creates the object and uses object instance data as POST. I have looked at the documentation and angular-resource.js and there doesn't seem to be a way to specify any default custom properties for the object being created by resource without modifying angular-resource.js.

What you can do is:

services.factory("Product", function($resource) {
    return$resource("http://someUrl", {}, {
        get   : {method: "GET", params: {productId: "-1"}},
        update: {method : "POST"}
    });
});

and in your controller:

$scope.product = {}; // your product data initialization stuff$scope.product.someDataKey = 'someDataValue'; // add your default datavar product = new Product($scope.product);
product.$update();

Solution 2:

I think it will depend on how you call the update function. If you read the angular main page's tutorial, under "Wire up a Backend", the mongolab.js provides a 'Project' factory. Copied verbatim:

angular.module('mongolab', ['ngResource']).
factory('Project', function($resource) {
  varProject = $resource('https://api.mongolab.com/api/1/databases' +
      '/angularjs/collections/projects/:id',
      { apiKey: '4f847ad3e4b08a2eed5f3b54' }, {
        update: { method: 'PUT' }
      }
  );

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

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

  returnProject;
});

The usage is that you first get an instance of the Project:

project = Project.get({id:1});

Then do an update after some changes:

project.update(someFunction);

In your case, you can change the update to always add the data you need:

Product.prototype.update = function(cb) {
  returnProduct.update({},
      angular.extend({}, this, {someDataKey: someDataValue}), cb);
};

Otherwise, you can most likely put the key/value pair in the params:

    update: {method : "POST", params:{someDataKey: someDataValue}}

It will be POSTed with the key/value pair in the URL, but most app servers nowadays will throw the pair into the params object anyway.

Solution 3:

I think most have missed a tiny gem in the documentation here.

non-GET"class" actions: Resource.action([parameters], postData, [success], [error])

This suggests you can do the following.

var User = $resource('/user');
postData = { name : 'Sunil', 'surname' : 'Shantha' };

var user = User.save({notify:'true'}, postData, function() {
  // success!
});

The second parameter when doing a save action (post) is post data.

Solution 4:

Wrapper function will work.

functionmyPost(data) {
  return$http.post('http://google.com', angular.extend({default: 'value'}, data))
}

myPost().success(function(response) { ... });

Solution 5:

Might this solve your problem?

services.factory("Product", function($resource) {
  return$resource("http://someUrl", {}, {
    get   : {method: "GET", params: {productId: "-1"}},
    update: {method : "POST", params:{}, data: {someDataKey: someDataValue}}
  });
});
services.factory("DefaultProduct", function(Product) {
  returnfunction(){
     returnnew Product({
        data:"default";
     });
  };
});
services.controller("ProductCTRL",function($scope,DefaultProduct){
  $scope.product = new DefaultProduct();
});

Post a Comment for "Default $resource Post Data"