javascript - How to create a Angularjs partial update (patch)? -


i new angular, have aplication manage customers data, json file stored in localstorage, how can create partial update patch(delta) method in service.

also need error handling.

json:

[     {"id":1,"name":"gigi", "lastname":"ds", "hobby":"football", "age":"1987/06/04"},     {"id":2,"name":"john", "lastname":"ciobanu", "hobby":"basball", "age":"2001/12/05"},     {"id":3,"name":"george", "lastname":"doe", "hobby":"rugby", "age":"2003/05/09"},     {"id":4,"name":"dean", "lastname":"smith", "hobby":"tenis", "age":"2000/03/06"},     {"id":5,"name":"kelly", "lastname":"ambrose", "hobby":"sweem", "age":"1986/09/12"} ] 

html:

<form name="part-update" novalidate>        <input type="number" class="number" name="id" value="{{customer.id}}" ng-model="customer.id" disabled />        <input type="text" name="name" value="{{customer.name}}" ng-model="customer.name" />        <input type="text" name="lastname" value="{{customer.lastname}}" ng-model="customer.lastname" />        <input type="text" name="hobby" value="{{customer.hobby}}" ng-model="customer.hobby" />        <input type="text" name="age" value="{{customer.age}}" ng-model="customer.age" />        <button class="btn btn-success btn-xs" ng-click="quicksave(customer)">save</button>        <button class="btn btn-danger btn-xs" ng-click="close()" >cancel</button> </form> 

controller:

....... $scope.quicksave=function(c){   customerdata.quickupdate(c);   $scope.quickedit = false;   $scope.customer={};   refresh(); } ....... 

service:

articleservices.factory("customerdata",["$http",'ls','$q','$filter','$log',function($http,ls,$q,$filter,$log){      var baseurl = 'jsondata/customers.json';      var dataload = null;       // throw('test');      init();      return{                 ............        //partial update          quickupdate: function(c){             return dataload.then(function(data){                   ???????????????????             });          }   }      function init(){          customers = ls.getdata("cutomers");          if(customers){             dataload = $q.resolve(customers);          }           else              dataload = $http.get(baseurl).then(function(response){                 ls.setdata(response.data,"cutomers");                  return response.data;             }).catch(function(e) { throw { status: e.staus, message: e.statustext }});             dataload.catch(onerror);           return dataload;               }       function onerror(error){          $log.error();          $log.error({ status: error.status, message: error.message, source: 'customerdata'});      }   }]); 

i found solution partial update

(angular.merge -> merge 2 objects)

var obj1 = {    'name': 'george',    'lastname': 'doe'  }    var obj2 = {    'name': 'gregor',    'age': '20'  }    var result = angular.merge({},obj1,obj2);    result={    'name': 'gregor',    'lastname': 'doe',    'age': '20'  }

for particular case:

quickupdate: function(c){      return dataload.then(function(data){         for(var i=0;i<data.length;i++){              if(data[i].id==c.id){                data[i] = angular.merge({},data[i],c);                break;              }          }        return data[i];      });    }

still need error handling :)


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -