Question

lubanlockServices.factory('UserConfig', ['$resource',
    function($resource){
        return $resource('user/config/:item', {item: '@item'});
    }
]);

lubanlockControllers.controller('NavCtrl', ['$scope', 'UserConfig',
    function($scope, UserConfig){
        $scope.minimized = UserConfig.get({item: 'nav_minimized'});

        $scope.$watch('minimized', function(value){
            console.log(value);
        });

        $scope.toggleMinimize = function(){
            $scope.minimized = !$scope.minimized;
            UserConfig.save({item: 'nav_minimized'}, $scope.minimized);
        }
    }
]);

The problem is, when $scope.minimized is false, the request will not contain any post data, rather than sending a JSONified false. That causes problem saving value in server side.

Any solution? Or is there something wrong with my usage?

Was it helpful?

Solution

JSON is Javascript Object Notation, you really should be POSTing (and retrieving) an object. Ideally, it would look like:

$scope.config = UserConfig.get({item: 'nav_minimized'}); 
//returns a $resource with data {minimized: false}

$scope.$watch('config.minimized', function(value){
    console.log(value);
});
$scope.toggleMinimize = function(){
    $scope.config.minimized = !$scope.config.minimized;
    $scope.config.$save(); //and then you can do this to save
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top