I have a private currJobs
member that I use inside of a service, and use a getter and setter method to access it.
Additionally, if the user chooses to edit a specific job, I use a 'lookup' function, that is supposed to return a copy of the job object(return a value - *), and then when the user saves the edited job, I use another mthod to replace the exsisting job object with the new one(and Patch request to the server of course)
Instead, it seems to return a reference(&) to the allegedly private 'currJobs' member, which causes an immediate change in the service with every change in the model. Which means that the object get updated on the frontend even if the use didn't press save.
I've searched Google and encountered this SO question, but it doesn't address the 'private service member, outside of the return{} construct' issue.
I was wondering how is it possible, and what would be a good, healthy and 'good practice' solution to this issue
code:
app.service('jobsService', function($http, $q, $location, $rootScope, companyService, utils){
var that = this;
that.currJobs = null;
var jobServ = {};
jobsServ.jobsLookup = function(jid, cid) {
if(!that.currJobs || !that.currJobs[cid] || that.currJobs[cid].length == 0){
return false;
}
for(var i = 0; i < that.currJobs[cid].length; i++){
if(jid == that.currJobs[cid][i].id){
var job = that.currJobs[cid][i];
return job;//the following returns a refference
}
}
return false;
},
jobsServ.replaceJob = function(jobData, cid){
if(!that.currJobs)
that.currJobs = {};
if(!that.currJobs[cid])
that.currJobs[cid] = [];
if(that.currJobs && that.currJobs[cid] && that.currJobs[cid].length == 0){
that.currJobs[cid].push(jobData);
return;
}
}