In nearly every software application, there is a necessity to modify objects
.
In my scenario, I have a list of objects
that can be edited on the fly with the help of Angular. However, this leads to a dilemma as exemplified by the following controller:
app.controller('editCategoryInstant', ['$http', '$scope', '$state', '$modalInstance', 'api', '$filter', 'category', 'LRM', function ($http, $scope, $state, $modalInstance, api, $filter, category, LRM) {
$scope.LRM = LRM;
$scope.category = category;
$scope.ok = function () {
$scope.category.color = $('#color').val();
category = $scope.category;
if ($scope.category.icon != null) {
$http({
url: api.getUrl('category', category.id),
method: "PUT",
data: {category: $scope.category}
}).success(function (data, status, headers, config) {
}).error(function (data, status, headers, config) {
var i = 0;
});
$modalInstance.dismiss('cancel');
}
else {
alert('Select icon');
}
};
$scope.cancel = function () {
$scope.category = $scope.master;
$modalInstance.dismiss('cancel');
};
$scope.clickUpload = function () {
setTimeout(function () {
angular.element('#fileUpload').trigger('click');
}, 0);
};
}]);
Let's simplify it.
I aim to modify an existing object that resembles the following:
var category = {
name: 'My Category',
icon: 'fa fa-bomb',
color: '#FFF',
description: 'This describes the category'
};
Then I pass the category
variable to the controller
The controller is connected to a modal
view structured as follows:
<div class="modal-header">
<h3 class="modal-title" translate="STRUCTURE.CATEGORY.CREATE"></h3>
</div>
<form role="form" class="form-validation" ng-submit="ok()">
<div class="modal-body">
<div class="row">
<div class="col-xs-12">
<label class="control-label">{{'TERMS.NAME' | translate}}</label>
<input type="text" placeholder="{{'TERMS.NAME' | translate}}" class="form-control"
ng-model="category.name" required>
</div>
<div class="col-xs-6" style="margin-top: 10px;">
<label>{{'LIBRARY.CATEGORY.SELECTICON' | translate}}</label>
<lb-icon-picker targetvariable="category"></lb-icon-picker>
</div>
<div class="col-xs-6" style="margin-top: 10px;">
<label class="block">{{'LIBRARY.CATEGORY.SELECTCOLOR' | translate}}</label>
<lb-color-picker targetvariable="category"></lb-color-picker>
</div>
<div class="col-xs-12" style="margin-top: 5px;">
<textarea class="form-control" style="height: 150px" placeholder="{{'TERMS.DESCRIPTION' | translate}}" ng-model="category.description"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-grey" ng-click="cancel()" tooltip="{{ 'TOOLTIP.CANCEL' | translate }}"><i
class="fa fa-ban"></i></a>
<button type="submit" class="btn btn-success" tooltip="{{ 'TOOLTIP.SAVE_AND_EXIT' | translate }}"><i
class="fa fa-check-square-o"></i></button>
</div>
</form>
During the editing process, due to two-way data binding
, any modifications made to the original object are reflected instantly. Therefore, if changes are canceled, the object will still appear altered.
Considering this circumstance, I attempted to research $apply
and copy
, but I am uncertain about their implementation in this context.
What is the recommended approach in the aforementioned scenario? How do you manage such situations?