javascript - AngularJS SPA using API and Mongoose -


i creating simple angularjs spa using api load data mongoose.

my app adds, displays , edits list of members. works when store members in array in factory service want change hook mongoose via api.

factory

app.factory('simplefactory', ['$http', function($http){      var factory = {};      var members =  $http.get('/api/members')        factory.getmembers = function ()      {         return members = $http.get('/api/members');     }     factory.getmember = function (index) {         if (index >=0 && index < members.length ) {            return  members[index] = $http.get('/api/members/' + member_id )        }        return undefined     }       factory.addmember = function(member) {          return $http.post('/api/members',member)     }     factory.updatemember = function(index,member) {          $http.put('/api/members/' + member_id, member)     }      return factory; }]) 

controller

app.controller('memberscontroller', ['$scope','simplefactory',    function ($scope,simplefactory) {       simplefactory.getmembers()     .success(function(members) {     $scope.members = members;     });   $scope.addmember = function() {     var member = {         name: $scope.newmember.name,         address: $scope.newmember.address,         age : $scope.newmember.age,         level : $scope.newmember.level,         swimmer : $scope.newmember.swimmer,         email : $scope.newmember.email,         regdate : $scope.newmember.regdate,                 }        simplefactory.addmember(member)       .success(function(added_member)        {           $scope.members.push(added_member);          $scope.newmember = { }       }       ); }  }]) 

but not sure how change controller updating member, coded follows pick members array in factory setting, how code pick members mongoose via api:

app.controller('memberdetailcontroller', ['$scope', '$location', '$routeparams', 'simplefactory',     function($scope, $location, $routeparams, simplefactory) {          $scope.member = {             index: $routeparams.member_index,             detail: simplefactory.getmember($routeparams.member_index)         }           $scope.updatemember = function() {             simplefactory.updatemember($scope.member.index,                 $scope.member.detail)             $location.path('/members')         }      } ]) 

can help, not complicated app i'm learning , stuck here!

thanks

you $scope.member object should set after getmember promise success.

code

simplefactory.getmember($routeparams.member_index).then(function(data){     $scope.member = {         index : $routeparams.member_index,        detail : data.user     }; }); 

apart need make sure getmember method should return promise while index 0

   factory.getmember = function (index) {         var deferred = $q.defer();         if (index >=0 && index < members.length ) {            return members[index] = $http.get('/api/members/' + member_id )        }        deferred.resolve;     } 

update

for calling update method need change service first return promise

factory.updatemember = function(index,member) {      return $http.put('/api/members/' + member_id, member) } 

then call factory.updatemember resolve promise , $location.path

$scope.updatemember = function() {     simplefactory.updatemember($scope.member.index, $scope.member.detail)     .then(function(data) {         $location.path('/members')     }); }; 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -