Issue
I use a REST Server API with angular $resource. I have no control over the Server API. The Server API requires that a list item has a property ItemTyp, which the list name with a Item appended. Which is redundant information
I would like to use the same resource for multiple lists and no. I tried following:
(function () {
'use strict';
angular.module('mod', ['ngResource']);
angular.module('mod').controller('Controller', function (ListItem) {
var listItem1 = new ListItem({ a: 1 });
listItem1.$save();
var listItem2 = new ListItem({ b: 2 });
listItem2.$save({ listName: 'list2' });
});
angular.module('mod').factory('ListItem', function ($resource) {
return $resource('/_api/lists/:listName/:Id', { listName: 'list1', Id: '@Id' },
{
save: {
method: 'POST',
transformRequest: function (data) {
//data.ItemTyp = listName + "Item";
return angular.toJson(data);
}
}
});
});
})();
But I found no way to get the params in the transformRequest. Is there a way to get the params or a other way to solve my problem?
Solution
You need a wrapper function to parse parameter into that object. Take a look at someData
. Btw. you don't need to create a new object like new ListItem()
.
(function () {
'use strict';
angular.module('mod', ['ngResource']);
angular.module('mod').controller('Controller', function (ListItem) {
ListItem({ data: 'test'}).save({Id: 3, listName: 'testName'});
ListItem({ data: 'test'}).save({Id: 4, listName: ''});
});
angular.module('mod').factory('ListItem', function ($resource) {
return function (someData) {
return $resource('/_api/lists/:listName/:Id', {
listName: '@listName',
Id: '@Id'
}, {
save: {
method: 'POST',
transformRequest: function (data) {
//data.ItemTyp = listName + "Item";
console.log(someData);
return angular.toJson(data);
}
}
}
);
}
});
})();
Answered By - lin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.