1

How can I make the object inherit all the properties from the other object.

This is the code:

this.makeReady = function(order) {
    var tempOrder = angular.copy(order);
    tempOrder.status = 1;
    angular.forEach(tempOrder.items, function(item){
        item.status = 1;
    })
    return $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){
        order = tempOrder; // this doesn't work
    });
}

In case of success: change the value of that object.

2
  • What are you passing into the order parameter of your makeReady function? If it's $scope.order, then just use $scope.order = tempOrder; in your success function. Commented Oct 10, 2014 at 18:05
  • $scope.allOrders contains all orders, so when i change order it will affect on allOrders.. Commented Oct 10, 2014 at 21:03

2 Answers 2

1

Try to directly edit the order in your $scope.allOrders and see if that gets you the behavior that you are looking for.

    this.makeReady = function (order) {
        var tempOrder = angular.copy(order);
        var orderIndex = $scope.allOrders.indexOf(order);
        tempOrder.status = 1;

        angular.forEach(tempOrder.items, function(item) {
            item.status = 1;
        });

        return $http.put('/rest/change/invoice/' + order.id + '/', tempOrder).success(function () {
            $scope.allOrders[orderIndex] = tempOrder;
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

0

use this

this.makeReady = function(order) {
    var tempOrder = angular.copy(order);
    tempOrder.status = 1;
    angular.forEach(tempOrder.items, function(item){
        item.status = 1;
    })
    $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){
        order = tempOrder; // this doesn't work
        return order;
    });
}

or use callBack function

  this.makeReady = function(order, callback) {
        var tempOrder = angular.copy(order);
        tempOrder.status = 1;
        angular.forEach(tempOrder.items, function(item){
            item.status = 1;
        })
        $http.put('/rest/change/invoice/'+order.id+'/', tempOrder).success(function(){
            order = tempOrder; // this doesn't work
            callback(order)
        });
    };

call function

this.makeReady({status:1, data:2, items:{status:1}}, function(data){
// this data your order variable in service
})

2 Comments

I'm not sure that your first example would work as expected. $http.put is not synchronous. The callback example should work.
First example doesn't work.. second example does not fit in my logic, becouse when i update this order, it will be updated on allOrders variable.. first example would be great if works..

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.