Skip to content

Inconsistent API for remote methods and hooks #597

Closed

Description

@bajtos

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:

Model.remoteMethod('foo', { isStatic: false, ... });

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.

Activity

  1. ritch commented on Sep 30, 2014

    @ritch
    Member

    I like this... the isStatic flag is very confusing when reading code using it.

  2. ritch commented on Sep 30, 2014

    @ritch
    Member

    All this really takes is to support prototype.methodName in sharedClass.find() (in strong-remoting)

  3. assigned and unassigned on Oct 3, 2014
  4. bajtos commented on Oct 3, 2014

    @bajtos
    MemberAuthor

    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 method
    loopback.Model.disableRemoteMethod('foo') // interpreted as a prototype method (!!)
    loopback.Model.sharedClass.find('foo') // interpreted as a prototype method (!!)

    Desired behaviour:

    // static methods
    loopback.Model.remoteMethod('foo', {}) 
    loopback.Model.disableRemoteMethod('foo') // breaking change
    loopback.Model.sharedClass.find('foo') // breaking change
    
    // prototype methods
    loopback.Model.remoteMethod('prototype.foo', {})
    loopback.Model.disableRemoteMethod('prototype.foo') 
    loopback.Model.sharedClass.find('prototype.foo')
  5. raymondfeng commented on Oct 3, 2014

    @raymondfeng
    Member

    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.

  6. added this to the LoopBack 3.0 milestone on Nov 26, 2014
  7. self-assigned this
    on Nov 26, 2014
  8. bajtos commented on Jan 6, 2015

    @bajtos
    MemberAuthor

    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.

  9. bajtos commented on Jan 20, 2016

    @bajtos
    MemberAuthor

    @0candy in reference to https://github.com/strongloop-internal/scrum-loopback/issues/617, here is what I would like you to implement:

    1. 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.
    2. Deprecate SharedClass.prototype.find in 3.0 only
    3. 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.
    4. 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).
    5. 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).
    6. (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.

    @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?

  10. bajtos commented on Aug 4, 2016

    @bajtos
    MemberAuthor

    @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?

  11. 0candy commented on Aug 4, 2016

    @0candy
    Contributor

    @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.

  12. ladaltamirano commented on Aug 15, 2016

    @ladaltamirano

    Please, document this to be able to understand it and use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions