loopback icon indicating copy to clipboard operation
loopback copied to clipboard

How to define method in relation?

Open Spark-star opened this issue 12 years ago • 10 comments

Hi, everyone. I want to know how to define method in relation?

In my app: A hasmany B, B blongsTo A ; The route "/a/:id/B/:fk" , Corresponding method is "updateById__bs" Can I define method "test" in relation "hasMany", that can display "/a/:id/B/:fk/test" which corresponding method is "test_bs", and how? THX

Spark-star avatar Nov 03 '14 09:11 Spark-star

The quick and dirty way is to write a custom method on Model A with http path ‘/:id/B/:fk/test’.

See http://docs.strongloop.com/display/LB/Remote+methods http://docs.strongloop.com/display/LB/Remote+methods

Thanks,


Raymond Feng Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop http://strongloop.com/ makes it easy to develop APIs http://strongloop.com/mobile-application-development/loopback/ in Node, plus get DevOps capabilities http://strongloop.com/node-js-performance/strongops/ like monitoring, debugging and clustering.

On Nov 3, 2014, at 1:10 AM, Spark-star [email protected] wrote:

Hi, everyone. I want to know how to define method in relation?

In my app: A hasmany B, B blongsTo A ; The route "/a/:id/B/:fk" , Corresponding method is "updateById__bs" Can I define method "test" in relation "hasMany", that can display "/a/:id/B/:fk/test" which corresponding method is "test_bs", and how? THX

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/728.

raymondfeng avatar Nov 03 '14 17:11 raymondfeng

Thanks Do you mean that I can't write a method on relation?

Spark-star avatar Nov 05 '14 03:11 Spark-star

@Spark-star sure you can do it, please see:

https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/relations.test.js#L3346

fabien avatar Nov 05 '14 06:11 fabien

Thank you for your answer.

How can i ensure the relation has been loaded when i define the relation.method ?

2014-11-05 14:02 GMT+08:00 Fabien Franzen [email protected]:

@Spark-star https://github.com/Spark-star sure you can do it, please see:

https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/relations.test.js#L3346

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/728#issuecomment-61763650.

Spark-star avatar Nov 05 '14 06:11 Spark-star

You should fetch() within that method, to access the related models. Is that what you mean?

fabien avatar Nov 05 '14 06:11 fabien

nope I mean how can I ensure the model in relation attached when i define the relation method.

2014-11-05 14:50 GMT+08:00 Fabien Franzen [email protected]:

You should fetch() within that method, to access the related models. Is that what you mean?

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/728#issuecomment-61766715.

Spark-star avatar Nov 05 '14 07:11 Spark-star

I still don't really know what you mean, but to get the relation definition, so you can add the method:

YourModel.relations['relationName'].defineMethod('summarize', summarize);

fabien avatar Nov 05 '14 07:11 fabien

For example, I defined the relation Category.hasMany( Product); when I define method on this relation in category.js, In some case ,the Product model would be not loaded.

I see the https://github.com/strongloop/loopback/issues/551 can load all the models, but it hasn't been merged.

2014-11-05 15:10 GMT+08:00 Fabien Franzen [email protected]:

I still don't really know what you mean, but to get the relation definition, so you can add the method:

YourModel.relations['relationName'].defineMethod('summarize', summarize);

— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/728#issuecomment-61768089.

Spark-star avatar Nov 05 '14 07:11 Spark-star

If you run into this issue, you can also do it in a boot script (in server/boot), and/or inside a Category.on('attached', function() { ... }) event.

fabien avatar Nov 05 '14 16:11 fabien

@Spark-star Has the issue been resolved? Can I close it?

superkhau avatar Feb 26 '15 03:02 superkhau