Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Angular $resource object to regular object without methods

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
            }
        }
    });
}]);
like image 644
Jason O. Avatar asked Jan 10 '15 23:01

Jason O.


1 Answers

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

like image 195
Peter Ashwell Avatar answered Sep 19 '22 23:09

Peter Ashwell