How to create a Remote Method programmatically?

247 views
Skip to first unread message

John Diddle

unread,
Aug 15, 2016, 2:15:21 AM8/15/16
to loopb...@googlegroups.com
Hi,
     I've read in these threads to figure it out how to define a Remote Method programmatically, but don't have a clue yet:

     Thank you,

Heath Morrison

unread,
Aug 15, 2016, 4:08:02 AM8/15/16
to LoopbackJS
Hi John,

The Remote Methods document you linked provides a number of very clear examples of this. Perhaps you can elaborate on what sort of problem you're having?

On Mon, Aug 15, 2016 at 9:15 AM John Diddle <acas...@gmail.com> wrote:
Hi,
     I've read in these threads to figure it out how to define a RemoteMethos programmatically, but don't have a clue yet:

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/792e6c4c-a62e-472e-9d88-055cb1406b09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Diddle

unread,
Aug 15, 2016, 2:15:27 PM8/15/16
to loopb...@googlegroups.com
Heath,
         I think you misunderstood in some way..., none of the documentation above provide examples of how to create a remote method programmatically, but they provide several examples of how declaratively define them..., I would need some function that let me define them dynamically, on demand... I searched threads in this group, issues tab in github and even stackoverflow, and the official documentation, but have not been successful.

         It's months since I've been trying to find something useful, but without success. I've even resent this post since last month noone replied.

         I'm open to code a pull request with this feature, but I really need guidance on how to do this first.

         Could @RaymondFeng please help me on this? Someone that let him know?

         I need concrete possible solutions for this. If I don't get them I would have to change of framework and I really don't want to. 
             Thank you all,

                   John

John Diddle

unread,
Aug 15, 2016, 2:25:09 PM8/15/16
to loopb...@googlegroups.com
Besides, in https://docs.strongloop.com/display/public/LB/REST+connector#RESTconnector-Definingacustommethodusingatemplate
an example shows how to invoke geocode..., but don't tell you how you should specify the method to invoke! What if you have several methods that accept the same type of parameters?!

""....

By default, LoopBack REST connector also provides an 'invoke' method to call the REST API with an object of parameters, for example:

Model.invoke({street: '107 S B St', city: 'San Mateo', zipcode: '94401'}, processResponse);

...""

No one have asked about this either...

      This relates to the need I have of being able to define remote methods programmatically/dinamically/on demand ... I would need a solution for this. Wouldn't like to change framework since Strongloop Loopback is so cool... please help me to solve this, I'm open to code a fork with this feature..

Akram Shehadi

unread,
Aug 15, 2016, 4:03:22 PM8/15/16
to LoopbackJS
I'm not really sure how would I implement something like this, but I'm curious about what you are trying to do

Just to understand it fully, what you need to do is somehow dynamically specify the code that should be executed if a given remote method is called?

So for example, you would have a remote method called:

myMutableMethod(param1, param2)

and depending on certain conditions (maybe based on param values or depending on certain query result from a DB or similar?) you would execute one of several implementations, let's say either 

myFirstImplementation(param1, param2)

or
  
myOtherImplementation(param1, param2)


Is this correct?


Because if it is, as far as I can tell you would need to either use eval() which I believe might be dangerous and slow, or something like https://stackoverflow.com/questions/584907/javascript-better-way-to-add-dynamic-methods

However I'm not sure how would that interact with StrongLoop. You would need to try and see.


Also, what exactly are you trying to solve? why is it that you need to dynamically create a remote method? maybe there is another solution to your problem instead of doing dynamic stuff, which I think could be a bit hard.

Akram

Heath Morrison

unread,
Aug 15, 2016, 4:34:52 PM8/15/16
to loopb...@googlegroups.com
Hello John,

It is difficult to advise you well without knowing a bit more about what you're trying to accomplish, but I can share a few thoughts.

Arguably the biggest benefit of Loopback is that it provides type management for your API. Most of the other functionality is built upon this - validation, the ORM, hooks, etc. Everything works with specific Models, including the REST interface. 

So when we talk about dynamic resolution of remote methods, what this implies is that you do not know in advance the data types that these remote methods are working with. This feels in conflict with much of the benefit of Loopback as a library.

That said - keep in mind that Loopback is built on top of Express, and with Express you can easily define a wildcard route that accepts whatever unstructured data you need to work with. From one of those route handlers you can even work with your Loopback models, if you like. 

Lastly, regarding this "Model.invoke" you found in the docs - this is specific to the REST connector, which facilitates access to remote REST resources, and I believe completely unrelated to what you're trying to do. The REST connector allows you to define remote methods, but all of these just map function arguments into parameters that are inserted into a URL for a remote request. Those remote methods basically use Model.invoke, which is a more direct mapping key/vals to URL parameters. 

Hope this helps.

-Heath

On Mon, Aug 15, 2016 at 9:25 PM John Diddle <acas...@gmail.com> wrote:
Besides, in 
an example shows how to invoke geocode..., but don't tell you how you should specify the method to invoke! What if you have several methods that accept the same type of parameters?!

""....

By default, LoopBack REST connector also provides an 'invoke' method to call the REST API with an object of parameters, for example:

Model.invoke({street: '107 S B St', city: 'San Mateo', zipcode: '94401'}, processResponse);

...""

No one have asked about this either...

      This relates to the need I have of being able to define remote methods programmatically/dinamically/on demand ... I would need a solution for this.
El lunes, 15 de agosto de 2016, 3:15:21 (UTC-3), John Diddle escribió:
Hi,
     I've read in these threads to figure it out how to define a Remote Method programmatically, but don't have a clue yet:

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.

John Diddle

unread,
Aug 15, 2016, 11:09:30 PM8/15/16
to LoopbackJS
Hi Akram, thank you for your attention,

       First, I would like to know how to invoke a function defined in a restfull dataSource. It's not clear to me from reading all this documentation:


       You see..., calling invoke(... does not specify the name of the function defined in the datasource.

       Second, what I'm trying to do is defining a remote method when calling a remote method... That's what I'm trying to do! And it this new remote method I want to call a function defined in a rest connector dataSource! Let's work together if you're interested on it! I can share with you the code!

John Diddle

unread,
Aug 15, 2016, 11:43:23 PM8/15/16
to LoopbackJS
Oh, than you Heath..., now I get how the  "Model.invoke" function works...

       So, I appreciate the remark that loopback wouldn't be the best choice to dinamically define remote methods..., I know you know about this things, what other framework you think would be useful for this? Do you know any working example I can download and play around with?

       Thank you, It has been useful,

             John
Reply all
Reply to author
Forward
0 new messages