简体   繁体   中英

Prevent from Angular scope variables to bind automatically to the service private members by reference

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;
    }
}

According to this , JavaScript passes anything that's not a primitive(int, bool, string) as a reference, so my encapsulation obviously doesn't work.

Added the following code:

app.factory('utils', function(){
  return{
            clone: function(obj) {
            if (null == obj || "object" != typeof obj) return obj;
            var copy = obj.constructor();
            for (var attr in obj) {
                if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
            }
            return copy;
        }
  }
}

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 = utils.clone(that.currJobs[cid][i]);//Fix the problem
            return job;
        }
    }
    return false;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM