samedi 27 juin 2015

Is it correct update client side object with angular.copy response

I have an import functionality client side page with display 'Save button' or 'Saved lable' depends on entity`s id.

enter image description here

Here is my save function: response of it is created on server side, stored into database and selected after that object (after save i'd like to display stored value, which one should already have id (auto_increment PK)):

enter image description here

So, after i saved order and got response with stored value, i need to update passed as argument order (to set order's id, so that's saving button gone invisible and saved label showed).

I did it with angular.copy(entity, order), but is it correct and are there other practices with applying and updating view's object from 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'));
        });
}

Aucun commentaire:

Enregistrer un commentaire