Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove $promise and $resolved from json object

I'm using $resource in angular to get json object and its structure is defined below

[
    {
        "id": 0,
        "name": "Type1"
    },
    {
        "id": 1,
        "name": "Type 2"
    }
]

after fetching the data .. console.log(jsonObject) gives me

[Resource, Resource, $promise: Object, $resolved: true]

How can I remove $promise & $resolved from the resulting object ? I tried angular.fromJson(json) but still I see that these objects still exist.

like image 681
user1184100 Avatar asked Feb 17 '14 14:02

user1184100


4 Answers

I'm looking for the same answer, but at this moment I know only this ugly hack:

To write a func like this:

function cleanResponse(resp) {
    return JSON.parse(angular.toJson(resp));
}

and call it in every single query (ugly, yep):

function getSmt() {
    Resource.get({}, function (data) {
        $scope.some = cleanResponse(data);
    }, function (responce) {
        //handle error
    })
}

I'll be happy if someone would teach me how to do it correctly

like image 152
Sergei Panfilov Avatar answered Nov 07 '22 06:11

Sergei Panfilov


Example from my project

Diary.getSharedWithMe(function(data) {
        delete data.$promise;
        delete data.$resolved;
        _self.sharedDiariesWithMe = data;
    }, function(error) {
        console.log(error)
    });
like image 22
callmejuggernaut Avatar answered Nov 07 '22 06:11

callmejuggernaut


From this answer, it looks that yourResource.toJSON() is readily available.

like image 4
Javarome Avatar answered Nov 07 '22 05:11

Javarome


Right, I've faced the same problem several times and always ended up using $http instead of $resource, however, today I decided to try and deal with this issue. I hope somebody will find this useful one day as well.

So, after you receive your object with $promise inside of it, what you do is just use angular.copy(data), like so:

someService.getSomething($scope.someId).$promise.then(function (data) {
    if (data) {
        $scope.dataWithoutPromise = angular.copy(data);
    }
});

After that, you will see that your dataWithoutPromise just contains the objects that you need, and $promise and $resolved are gone.

like image 3
Arthur S. Avatar answered Nov 07 '22 05:11

Arthur S.