Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  87] [ 6]  / answers: 1 / hits: 28610  / 12 Years ago, thu, june 28, 2012, 12:00:00

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 ?



EDIT :



Well, i want to do something like this :



/**
* Module declaration.
* @type {Object}
*/
var services = angular.module(services, [ngResource]);

/**
* Product handler service
*/
services.factory(Product, function($resource) {
return $resource(http://someUrl, {}, {
get : {method: GET, params: {productId: -1}},
update: {method : POST, params:{}, data: {someDataKey: someDataValue}}
});
});


Where data is the default data for my future POST requests.


More From » angularjs

 Answers
66

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 data

var product = new Product($scope.product);
product.$update();

[#84604] Wednesday, June 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gleng

Total Points: 471
Total Questions: 107
Total Answers: 102

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;