restangular patch nested field

Issue

I’m having troubles patching a nested object with Restangular.

The object I’m patching to:

{
"unread_notification_count": 18, 
"notification_settings": {
    "mention": {
        "email_notification": true, 
        "platform_notification": true, 
        "desktop_notification": true
    }, 
    "history_update": {
        "email_notification": true, 
        "platform_notification": true, 
        "desktop_notification": true
    }, 
    "assign": {
        "email_notification": true, 
        "platform_notification": true, 
        "desktop_notification": true
    }
}

I want to patch some of the booleans when I toggle a switch.

When I toggle a switch right now I’m fire this function

$scope.patch = function(key, field, value) {
         var updated = {
         };
        // Below this isnt working
        updated['notification_settings'][key][field] = value;

         Restangular.one('users/'+ Global.user.id).patch(updated).then(function (data) {
             console.log('succes');
         }, function(error) {
             console.log(error);
         });
    }

The part I can’t seem to get working is what to post in the updated object. I need to update a nested field, but I don’t know how.

For example I want to set the notification_settings -> mention(key) -> platform_notification(field) = true(value)

Solution

I found the answer for anyone who is having the same problem 😀

in the updated object I just added the whole notification settings

var updated = {
    notification_settings: Global.user.notification_settings
};

Restangular did the rest for me. This isn’t the best way to do this I think, but Instead of posting 1 boolean to the server I’m posting 9. Performance wise this isn’t a big issue (for me)

Answered By – Xiduzo

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.