2014-09-19 4 views
1

안녕하세요. 각도를 통해 mongodb 컬렉션에서 사용자를 찾고 업데이트해야합니다. 은 내가 _id에 의해 사용자 이름으로 그들을 찾을 필요가있다, 그래서 나는이 같은 서비스 생성 :

// Users service used for communicating with the users REST endpoint 
angular.module('users').factory('Users', ['$resource', 
    function($resource) { 
     return $resource('users/:id', {}, { 
      update: { 
       method: 'PUT' 
      } 
     }); 
    } 
]); 

을 그리고 익스프레스에 내가 상대 API 경로가 : 이제

app.route('/users/:userId').put(users.update); 

을, 내가 가진 가정 다음과 같은 다른 특급 경로로 사용자 이름 가용성을 확인하십시오.

app.route('/users/:username').get(users.check); 

어떻게이 마지막 서비스를 동일한 각도 서비스에 통합 할 수 있습니까?

업데이트 : 해결 되었습니까? 맞습니까?

angular.module('users').factory('Users', ['$resource', 
    function($resource) { 
     return { 
      byId: $resource('users/:id', {}, { 
       update: { 
        method: 'PUT' 
       } 
      }), 
      byUsername: $resource('users/:username', {}, { 
      }) 
     }; 
    } 
]); 
+0

당신이 명시 적 측면을 구별 할 방법은 두 노선들은 나에게 동일 본다. 두 번째 경로가 항상 가용성을 확인하는 데 사용됩니까? – Chandermani

답변

3

다음과 같이 하시겠습니까?

각도 서비스 :

angular.module('users').factory('Users', function($resource) { 
var resource = $resource('users/:byAttr/:id', {}, { 
    update: { 
     method: 'PUT', 
     isArray: false, 
     cache: false 
    } 
}); 
return { 
    updateById: function (id) { 
     resource.update({id: id, byAttr: 'id'}); 
    }, 
    updateByName: function (username) { 
     resource.update({username: username, byAttr: 'username'}); 
    }, 
} 

});

경로 :

app.route('/users/id/:userId').put(users.update); 
app.route('/users/user/:username').get(users.check); 
+0

네, 고마워요, 이것도 유용합니다. –