javascript - Is it correct update client side object with angular.copy response -
i have import functionality client side page display 'save button' or 'saved lable' depends on entity`s id.
here save
function: response of created on server side, stored database , selected after object (after save i'd display stored value, 1 should have id
(auto_increment pk)):
so, after saved order , got response stored value, need update passed argument order (to set order's id, that's saving button gone invisible , saved
label showed).
i did angular.copy(entity, order)
, correct , there other practices applying , updating view's
object response.
code here:
<tbody> <tr ng-repeat="order in model.importedorders"> <td ng-bind="$index + 1"></td> <td ng-bind="order.externalorderid"></td> <td ng-bind="order.customerfullname"></td> <td ng-bind="order.customeraddress"></td> <td ng-bind="order.customerphone"></td> <td ng-bind="order.orderitems[0].item.id"></td> <td ng-bind="order.orderitems[0].purchaseprice.format('$n')" nowrap></td> <td ng-bind="order.orderitems[0].sellingprice.format('$n')" nowrap></td> <td ng-bind="order.vendor.id"></td> <td ng-bind="order.manager.id"></td> <td ng-bind="order.driver.id"></td> <td ng-bind="order.deliveryprice.format('$n')" nowrap></td> <td ng-bind="order.note"></td> <td> <a ng-if="!order.id" ng-click="actions.save(order)" href="#" class="btn btn-xs btn-primary">Сохранить</a> <span ng-if="order.id" class="label label-success">Сохранено</span> </td> </tr> </tbody>
js function;
save: function(order) { order$.save({ entity: order }).then( function(msg) { var entity = coalesce(msg, 'data.order'); angular.copy(entity, order); }, function(msg) { toaster$.pop('error', 'Ошибка на этапе сохранения заказа', coalesce(msg, 'data.description')); }); }
a little helper function:
function coalesce(obj, path) { return $parse(path)(obj); }
Comments
Post a Comment