0

I have page with table and data shows via ng-repeat and ng-model. When I click save button I call post service and that posts my data to db and returns me id in callback. How I can save response to my object?

in controller:

$scope.saveTask = function (workflow) {
    if (workflow.isSaved == false) {
        workflow.isSaved = true;
        $scope.Task.push($scope.newTask());
        restService.postArbitraryTask(angular.toJson(workflow));
    }
};

in rest-service:

postArbitraryTask: function (model, callback) {
    $http({
        method: 'POST',
        url: Global.api.saveArbitraryTask,
        contentType: 'application/json; charset=utf-8',
        data: model
    }).success(function (response) {
        console.log(response);
        if (callback) {
            callback(response);
        }
    }).error(function() {});
},

2 Answers 2

1

The postArbitraryTask function accepts a callback to which the response will be passed.

Use it:

restService.postArbitraryTask(
    angular.toJson(workflow),
    function(response){
        $scope.myObject = response;
    }
);

Also, instead of:

if (workflow.isSaved == false) {

You can use:

if (!workflow.isSaved) {

This behaves basically the same.

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

Comments

1

your postArbitraryTask accepts a callback and passes the response to it

postArbitraryTask: function (model, callback) {
    $http({
        method: 'POST',
        url: Global.api.saveArbitraryTask,
        contentType: 'application/json; charset=utf-8',
        data: model
    }).success(function (response) {
        console.log(response);
        if (callback) {
            callback(response);
        }
    }).error(function() {});
},

you can modify your saveTask function to use whatever data is returned from the server.

$scope.saveTask = function (workflow) {
    if (workflow.isSaved == false) {
        $scope.Task.push($scope.newTask());
        restService.postArbitraryTask(angular.toJson(workflow),
           function(response){
              workflow.isSaved = true;
              workflow.id = response.id;
           }
        );
    }
};

you can further modify your code to also handle errors (also success and error are deprecated):

postArbitraryTask: function (model, callback) {
    $http({
        method: 'POST',
        url: Global.api.saveArbitraryTask,
        contentType: 'application/json; charset=utf-8',
        data: model
    }).then(function (response) {
        console.log(response);
        if (callback) {
            callback(false,response);
        }
    },function(response) {
        callback(true,response);
    });
},

modify your callback to also accept errors

$scope.saveTask = function (workflow) {
    if (workflow.isSaved == false) {
        $scope.Task.push($scope.newTask());
        restService.postArbitraryTask(angular.toJson(workflow),
           function(error,response){
              if(!error){ 
                  workflow.isSaved = true;
                  workflow.id = response.data.id;
              }
              else{
                  //handle error here(display an alert?) if an error occurs or server responds with an error code such as 404 or 500
              }
           }
        );
    }
};

Comments

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.