Repository navigation
Feature - Add possibility to export shared methods to client models - #25
Conversation
|
Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test." |
|
@slnode add to whitelist. ok to test. |
|
All is well |
|
Hi @pandaiolo, thank you for the pull request. 1. Implementation MyModel.prototype.getUrl = function() {
return '/#/mymodels/' + this.id;
};
MyModel.prototype.getUrl.sharedCode = true;@ritch You were doing a lot of work on sharing models between the javascript client and the server. What's your opinion on this? 2. Tests Each unit-test should be self-describing, it must not depend on things configured elsewhere (e.g. on the server, see your changes in An example showing how the API can look like: given.servicesForLoopBackApp(
{
models: {
MyModel: { name: { type: String, required: true } }
},
methods: {
'MyModel.prototype.getUrl': {
code: (function() { return '/#/mymodels/' + this.id; }).toString(),
meta: { sharedCode: true }
}
}
}); |
There was a problem hiding this comment.
You should not access app.models directly, all data needed for the code generator must be available via RestClass, RestMethod and/or SharedClass and SharedMethod.
// not tested, just an idea
function getAllSharedCodeMethods(meta) {
var proto = meta.sharedClass.ctor.prototype;
return Object.keys(proto)
.map(function(k) { return proto[k]; })
.filter(function(m) {
return typeof(m) == 'function' && m.sharedCode === true;
});
} To make the implementation complete, you should support shared-code methods defined on the constructor too (e.g. MyModel.getUrlForId(id) { /*...*/})
There was a problem hiding this comment.
Thanks for the detailed review ! I can work on the PR to implement your
suggestions and make it more compliant with the rest of the project, but
let me know first if this is not already a WIP on SL side. If so, I suppose
there is no reason to merge. Otherwise I'll get it done. Thanks
2014-03-17 8:51 GMT+01:00 Miroslav Bajtoš notifications@github.com:
In lib/services.template:
@@ -214,6 +214,11 @@ postData.forEach(function(arg) { -%>
<% } -%>
}
);
+<% if (app.models[modelName] && app.models[modelName].prototype.clientMethods) app.models[modelName].prototype.clientMethods.forEach(function(fct) { -%>
- res.prototype['<%- fct %>'] = <%- app.models[modelName].prototype[fct] %>;
+<% }); // if meta.clientMethods foreach -%>You should not access app.models directly, all data needed for the code
generator must be available via RestClasshttps://github.com/strongloop/strong-remoting/blob/87ea65685ab9ff442b7769af5e4f24fdacd1fedb/lib/rest-adapter.js#L366,
RestMethodhttps://github.com/strongloop/strong-remoting/blob/87ea65685ab9ff442b7769af5e4f24fdacd1fedb/lib/rest-adapter.js#L386and/or
SharedClasshttps://github.com/strongloop/strong-remoting/blob/87ea65685ab9ff442b7769af5e4f24fdacd1fedb/lib/shared-class.jsand
SharedMethodhttps://github.com/strongloop/strong-remoting/blob/87ea65685ab9ff442b7769af5e4f24fdacd1fedb/lib/shared-method.js
.// not tested, just an ideafunction getAllSharedCodeMethods(meta) {
var proto = meta.sharedClass.ctor.prototype;
return Object.keys(proto)
.map(function(k) { return proto[k]; })
.filter(function(m) {
return typeof(m) == 'function' && m.sharedCode === true;
});}To make the implementation complete, you should support shared-code
methods defined on the constructor too (e.g. MyModel.getUrlForId(id) {
/.../})—
Reply to this email directly or view it on GitHubhttps://github.com//pull/25/files#r10647661
.
Sharing the code of model methods between the server and AngularJS clients is not on our short-term roadmap. It would be great if you could improve your pull request and contribute the feature yourself. |
|
This an interesting approach, but I think coming at it from the wrong angle. I am working on porting LoopBack entirely to the browser. This means you could share your code outright and glue the server and client together with a new type of data source (a LoopBack server data source). This requires some significant changes to strong remoting: Take a look at a simple example in the remoting-connector PR: https://github.com/strongloop/loopback/tree/feature/remoting-connector/example/client-server |
|
@ritch I still would like to land this feature, because it will take some time before the browser port of LoopBack is done and ready for the AngularJS SDK. If I understand it correctly, you won't need In which case, adding |
|
+1 On Mon, Mar 17, 2014 at 11:29 PM, Miroslav Bajtoš
StrongLoop http://strongloop.com/_\* makes it easy to develop APIs |
|
@pandaiolo How is the implementation going, is there anything I can help you with? |
|
Hi, I will be able to allocate some time for it next week, this one's too 2014-03-24 8:52 GMT+01:00 Miroslav Bajtoš notifications@github.com:
|
|
Test Failed. To trigger a build add comment - ".test\W+please" |
|
Test Failed. To trigger a build add comment - ".test\W+please" |
|
Test Failed. To trigger a build add comment - ".test\W+please" |
This PR adds the possibility for server models exported to Angular ressources, to define specific methods which will be exported as well to be exposed in the client.
The models need to specify the shared methods in an array property called
clientMethodsExample :
After using loopback-angular with the above model, the
getUrlmethod will be made available on the client side for every instance ofMyModelobjectsNote: I added tests to the specs for this behavior.