7

I am starting to learn AngularJS $resource, and noticed the $resource object has a few methods (see below for examples) attached to my data downloaded from server. How do I remove these methods and convert the object to a regular (array) object?

__proto__: Resource $delete: function (params, success, error) {$get: function (params, success, error) {$query: function (params, success, error) {$remove: function (params, success, error) {$save: function (params, success, error) {constructor: function Resource(value) {toJSON: function () {__proto__: Object

For instance, I'm trying to send a POST request including some key value data using $resource.save, but these 'proto' items in the array is somehow causing the data to become "undefined" when passed to $.param(data) in the factory. I could do the same thing using $http with ease, but want to learn $resource. Thanks!

Inside a Controller

 $scope.ok = function () {
        $scope.entry = new calEntry();  
        $scope.entry.data = data    // data is $resource object including _proto_ items
        $scope.entry.$save( function(){
            toaster.pop('success','Message','Update successfully completed.');
        });
    };

Factory

myApp.factory("calEntry",['$resource','$filter', function($resource, $filter) {

    return $resource("/griddata/", {}, {
        save: {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(data, headersGetter) {
                return $.param(data);      // data is undefined when it reaches here
            }
        }
    });
}]);

1 Answer 1

10

Try the toJSON function, it will fetch the data and remove the extra properties.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. I tried to change the $scope.entry.data = data.toJSON() but the proto properties are all still there. Am I missing something?
angular.toJson(data) does drop the properties and return a simple JSON string. Thank you.

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.