javascript - Angular way to differ server model and client view model -


i'm wondering best practice view model data server in angular app?

for example in controller i'm getting model server (with custom ngresource service) , need create somekind different model in app's clientside:

customapiservice.query({ url: 'items' }, function (res) {    // made , in partials "ng-repeat"?!    $scope.items = res;     // need model server data on client...    angular.foreach(res, function (key, i) {      $scope.viewmodel = {         description: key.anothernamefield // e.g. different third-party services return description under different names      };    }); }); 

what such case not use angular resource directly within controller. create our own service

approot.factory('modelservice', ['$resource', '$q', function ($resource, $q) { function modelclass() {    this.propertyone=null;    this.propertytwo=null;  } var serviceobj= {       getdata: function () {          $resource('url').query(function(data) {                var modellist=[];                //do foreach on each record in 'data' , create 'modelclass' object , map properties                return modellist;          });       }    }   return serviceobj; }]); 

this allows more control on gets exposed controller , hence view. ownership of model here service.

since same service can injected everywhere, not have custom mapping again , again.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -