You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent API for remote methods and hooks #597
As of LoopBack 2.0, there is a new method for defining remote method metadata Model.remoteMethod. To configure a prototype method, one has to supply a flag isStatic:
A) This is inconsistent with the approach we have already in place for hooks (docs), where we prefix the method name with prototype.:
Model.beforeRemote('prototype.foo', ...);
B) The usage of this API - e.g. ('find', true) - is very ambiguous. What does true mean? One has to be very familiar with the API to understand what is the intent of such code snippet.
We should modify Model.remoteMethod to support the prototype.foo notation too.
Since both Model.remoteMethod and Model.beforeRemote are thin wrappers around strong-remoting API, we should fix this problem in strong-remoting too.
I briefly looked at the code in strong-remoting and realised that this feature will be more difficult than I originally thought, at least if we want to preserve backwards compatibility.
The major problem is that the current implementation evaluates isStatic as false by when it is not present.
Current behaviour:
loopback.Model.remoteMethod('foo',{})// interpreted as a static methodloopback.Model.disableRemoteMethod('foo')// interpreted as a prototype method (!!)loopback.Model.sharedClass.find('foo')// interpreted as a prototype method (!!)
For the backward compatibility, what about allowing disableRemoteMethod/find to try both prototype and static methods? It's fairly rare to have conflicting names for static and prototype methods, especially for remoting.
Actually, I find sharedClass.find name rather ambiguous, a better name would be sharedClass.findMethod. That reveals a backwards-compatible solution: create a new method sharedClass.findMethod accepting a single string (prototype.foo or foo) and deprecate the find method.
Unfortunately fixing disableRemoteMethod is still a breaking change.
Create a new function SharedClass.prototype.findMethod that could be invoked like findMethod('create') and findMethod('prototype.updateAttributes') - the first call should find a static method, the second call a prototype method. Implement this function in both 2.x and 3.0 branches.
Deprecate SharedClass.prototype.find in 3.0 only
Modify SharedClass.prototype.disableRemoteMethod to accept a single string argument (to follow the signature of findMethod), in 3.0 only - this is a breaking change that needs an entry in 3.0 release notes.
Compile a list of other places that use isStatic when searching for a method, so that we can discuss whether we should change them too (3.0 only).
Take a look at places in LoopBack (Model, PersistedModel) that are affected by these API changes, rework the LoopBack API to follow the new strong-remoting API (3.0 only).
(This may be a part of the previous item.) Take a look at how remoting metadata is specified in common/models/my-model.json. IIRC, at the moment we require isStatic to be always present, to prevent confusion. With the new changes in place, we can remove that constraint and allow users to encode the flag static/prototype in the method name. See the discussion in Define remote methods using json schema definitions. Prototype for #209 #1163 for more details.
@bajtos Yes, this story can be closed. Earlier, I didn't close it as the modules weren't released yet. But just confirmed that they have all been merged and released. Closing this story.
Activity
ritch commentedon Sep 30, 2014
I like this... the
isStaticflag is very confusing when reading code using it.ritch commentedon Sep 30, 2014
All this really takes is to support
prototype.methodNameinsharedClass.find()(in strong-remoting)bajtos commentedon Oct 3, 2014
I briefly looked at the code in strong-remoting and realised that this feature will be more difficult than I originally thought, at least if we want to preserve backwards compatibility.
The major problem is that the current implementation evaluates
isStaticasfalseby when it is not present.Current behaviour:
Desired behaviour:
raymondfeng commentedon Oct 3, 2014
For the backward compatibility, what about allowing disableRemoteMethod/find to try both prototype and static methods? It's fairly rare to have conflicting names for static and prototype methods, especially for remoting.
bajtos commentedon Jan 6, 2015
Actually, I find
sharedClass.findname rather ambiguous, a better name would besharedClass.findMethod. That reveals a backwards-compatible solution: create a new methodsharedClass.findMethodaccepting a single string (prototype.fooorfoo) and deprecate thefindmethod.Unfortunately fixing
disableRemoteMethodis still a breaking change.bajtos commentedon Jan 20, 2016
@0candy in reference to https://github.com/strongloop-internal/scrum-loopback/issues/617, here is what I would like you to implement:
SharedClass.prototype.findMethodthat could be invoked likefindMethod('create')andfindMethod('prototype.updateAttributes')- the first call should find a static method, the second call a prototype method. Implement this function in both 2.x and 3.0 branches.SharedClass.prototype.findin 3.0 onlySharedClass.prototype.disableRemoteMethodto accept a single string argument (to follow the signature offindMethod), in 3.0 only - this is a breaking change that needs an entry in 3.0 release notes.isStaticwhen searching for a method, so that we can discuss whether we should change them too (3.0 only).common/models/my-model.json. IIRC, at the moment we requireisStaticto be always present, to prevent confusion. With the new changes in place, we can remove that constraint and allow users to encode the flag static/prototype in the method name. See the discussion in Define remote methods using json schema definitions. Prototype for #209 #1163 for more details.@0candy could you perhaps convert the above list into a checklist in https://github.com/strongloop-internal/scrum-loopback/issues/617, so that we can more easily track the progress?
bajtos commentedon Aug 4, 2016
@0candy what's the status of this story? I think most of the items outlined above have been already finished.
strongloop-internal/scrum-loopback#617 mentions few remaining items, but they seem to be finished too, judging by linked issues.
I think we can close this story as done now, what do you think?
0candy commentedon Aug 4, 2016
@bajtos Yes, this story can be closed. Earlier, I didn't close it as the modules weren't released yet. But just confirmed that they have all been merged and released. Closing this story.
ladaltamirano commentedon Aug 15, 2016
Please, document this to be able to understand it and use it.