1+ //copied in its entirity from loopbacks Model class.
2+ //it was necessary to do so as this function is used
3+ //in the other code below
14function convertNullToNotFoundError ( toModelName , ctx , cb ) {
25 if ( ctx . result !== null ) return cb ( ) ;
36
@@ -9,12 +12,30 @@ function convertNullToNotFoundError (toModelName, ctx, cb) {
912 cb ( error ) ;
1013}
1114
12- function fixHttpMethod ( fn , name ) {
13- if ( fn . http && fn . http . verb && fn . http . verb . toLowerCase ( ) === 'put' ) fn . http . verb = 'patch' ;
14- }
15-
1615module . exports = function ( app , options ) {
16+
17+ //iterate over all loopback models. This gives us a constructor function
18+ //and allows us to overwrite the relationship methods:
19+ // - belongsToRemoting
20+ // - hasOneRemoting
21+ // - hasManyRemoting
22+ // - scopeRemoting
23+ // - etc
24+ //
25+ //Note: This does not seem ideal.
26+ //We are copying a tonne of code out of loopbacks Model class
27+ //and then modifying little bits of it below.
28+ // see: https://github.com/strongloop/loopback/blob/master/lib/model.js#L462-L661
29+ //Most likely this will be fragile and
30+ //require us to keep up to date with any chances introduced in loopback.
31+ //
32+ //It would be good to have a discussion with the strongloop guys and see
33+ //if there is a better way this can be done.
1734 app . models ( ) . forEach ( function ( ctor ) {
35+
36+ //sets up remote relationship method belongsTo on any model that
37+ //has a belongsTo relationship defined in its .json file (or via the
38+ //node API)
1839 ctor . belongsToRemoting = function ( relationName , relation , define ) {
1940 var modelName = relation . modelTo && relation . modelTo . modelName ;
2041 modelName = modelName || 'PersistedModel' ;
@@ -83,6 +104,10 @@ module.exports = function (app, options) {
83104 } , findHasManyRelationshipsFunc ) ;
84105
85106 var createRelationshipFunc = function ( cb ) {
107+ //TODO: implement this
108+ //this is where we need to implement
109+ // POST /:model/:id/relationships/:relatedModel
110+ //
86111 // this['__get__' + pathName](cb);
87112 } ;
88113 define ( '__createRelationships__' + relationName , {
@@ -95,6 +120,10 @@ module.exports = function (app, options) {
95120 } , createRelationshipFunc ) ;
96121
97122 var updateRelationshipsFunc = function ( cb ) {
123+ //TODO: implement this
124+ //this is where we need to implement
125+ // PATCH /:model/:id/relationships/:relatedModel
126+ //
98127 // this['__get__' + pathName](cb);
99128 } ;
100129 define ( '__updateRelationships__' + relationName , {
@@ -107,6 +136,10 @@ module.exports = function (app, options) {
107136 } , updateRelationshipsFunc ) ;
108137
109138 var deleteRelationshipsFunc = function ( cb ) {
139+ //TODO: implement this
140+ //this is where we need to implement
141+ // DELETE /:model/:id/relationships/:relatedModel
142+ //
110143 // this['__get__' + pathName](cb);
111144 } ;
112145 define ( '__deleteRelationships__' + relationName , {
@@ -156,5 +189,12 @@ module.exports = function (app, options) {
156189 } ;
157190 } ) ;
158191
192+ //copied from loopbacks Model class.
193+ function fixHttpMethod ( fn , name ) {
194+ if ( fn . http && fn . http . verb && fn . http . verb . toLowerCase ( ) === 'put' ) fn . http . verb = 'patch' ;
195+ }
196+
197+ //iterate through all remote methods and swap PUTs to PATCHs
198+ //as PUT is not supported by JSON API.
159199 app . remotes ( ) . methods ( ) . forEach ( fixHttpMethod ) ;
160200} ;
0 commit comments