Vote count:
0
Im finding myself fighting with 'pointers' in javascript. It's like I'm in C++ again. I would like to know your approach to the following issue Im having.
My case: I have a ng-repeat that goes over a collection . When clicking one of the collection elements, I do a copy of the element , do changes over it and POST/PUT data to server .
If the server replies 200 then I will apply those changes to the collection . The collection contains objects, so it means that Im working with references NOT values.
The module definition with a service:
angular.module('dataModule', [])
.service('DataService', function () {
this.collection = [{id:0, who: 'I'},
{id:1, who: 'YOU'},
{id:2, who: 'HE'},
{id:3, who: 'SHE'},
{id:4, who: 'IT'}];
})
And here the controller:
.controller('listCtrl', function ($scope, $timeout, DataService) {
// Data used in view
$scope.collection = DataService.collection;
// Action trigger from the view
$scope.change = function(data, index){
// Get a copy
var copy = angular.copy(data);
// Apply changes over the copy
copy.id = data.id*100*index;
// Simulate POSTing/Updating data to server
console.log('Sending data to server...');
$timeout(function(){
// Response is 200
var response = 200;
// Assigning copy -> data
data = copy;
// The prev. assignment is not updating the collection
// Of course I could do $scope.collection[index] = copy;
// because this case is simple enough.
// Im finding myself having the service with methods find, edit, ...
// What's a better approach?
}, 2000)
}
});
As I said in the comments, Im finding myself implementing functions like find, edit or get in the service. Is that the approach to go???
Here is the jsfiddle http://ift.tt/1mnax4w in case I was not able to explain it correctly.
Aucun commentaire:
Enregistrer un commentaire