How to define method in relation?
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
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.
Thanks Do you mean that I can't write a method on relation?
@Spark-star sure you can do it, please see:
https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/relations.test.js#L3346
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.
You should fetch() within that method, to access the related models. Is that what you mean?
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.
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);
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.
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.
@Spark-star Has the issue been resolved? Can I close it?