javascript - Separate instance of angular factory/service -


i have factory function has data , functions manipulate it. data used several times in application, @ points data on page , opened in modal- manipulating data in modal changes data in background page. 'angular way' create separate instances of data?

updated:

the factory:

factory('filterfactory', [function () {   return {     getgroups: function(term){         // todo: terms groups $http         if(term){             // terms         }         else {             return this.terms;         }     },     addgroup: function(group){         (var term in this.terms) {             if(this.terms[term].name === group){                 this.terms[term].included = true;             }         }     },     removegroup: function(group){         (var term in this.terms) {             if(this.terms[term].name === group){                 this.terms[term].included = false;             }         }     },     selectgroup : function(group){         (var term in this.terms) {             if(this.terms[term].name === group){                 this.terms[term].included = true;             } else {                 this.terms[term].included = false;             }         }     },     setall : function(value){         (var term in this.terms) {             this.terms[term].included = value;         }     },      iscollapsed: true,      terms: {         people: {             included: false,             name: 'person',         },         organizations: {             included: false,             name: 'organization',         },         ...     }  }; 

}]).

attempted implementations:

    $scope.newtermmeta.type = filterfactory;  var temp = filterfactory; $scope.newtermmeta.type = temp.terms;  $scope.newtermmeta.type = filterfactory.getgroups();  var temp = filterfactory; $scope.newtermmeta.type = temp.terms;  $scope.newtermmeta.type = object.create(filterfactory.getgroups()); 

note: none of above implementations created isolated instance.

template code:

<div class="modal-body">      <form>         <label>              <h2>{{newtermmeta.name}}</h2>         </label>         <br>         <span>add term relevant term groups:</span>         <br>         <div class="well well-sm col-sm-8">             <button ng-repeat="group in newtermmeta.type" btn-checkbox class="btn btn-sm btn-default margin5" type="button" ng-model="group.included">{{group.name}}</button>         </div>         <br>         <span>optional:</span>         <br>         <label> enter concise (at 1 sentence) definition term:</label>         <input class="form-control width80p" type="text" ng-model="newtermmeta.definition">     </form> </div> 

the factory object shared instance, change in factory object going change using it.

factory right thing do, sounds want encapsulate in different scope. easy angular since controllers own scope , directives have option of having own scopes well. assuming showing in controller this:

myapp.controller('otherctrl', [ 'myfactory', '$scope', function(myfactory,$scope) {    // create defensive copy of factory object creating new object wrapped object prototype chain, can access data changing properties change on $scope.theobject directly   $scope.theobject = object.create(myfactory.gettheobject());   // alternatively, copy relevant properties our scope, it's protected won't updated when factory object changes unless specify $watch statement  var obj = myfactory.gettheobject();  $scope.name = obj.name;  }]); 

updated: caveat

one caveat when using object.create() defensive copy mechanism json stringify properties modify. not work if intend modify property submit entire object server. work great read-only properties, or serializing modified properties however. , angular update values of non-modified properties without $watch since can still detect changes via prototype chain.

a fiddle demonstrating difference between json.stringify , prototype chain values here http://jsfiddle.net/xxdqv/


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -