Skip to content

Feature - Add possibility to export shared methods to client models - #25

Merged
bajtos merged 0 commit into
strongloop:masterfrom
pandaiolo:master
Apr 8, 2014
Merged

bajtos merged 0 commit into
strongloop:masterfrom
pandaiolo:master

Conversation

@pandaiolo

Copy link
Copy Markdown
Contributor

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 clientMethods

Example :

MyModel = app.model('Brand', schema);

MyModel.prototype.getUrl = function() {
    return '/#/mymodels/' + this.id;
};

MyModel.prototype.clientMethods = ['getUrl'];

After using loopback-angular with the above model, the getUrl method will be made available on the client side for every instance of MyModel objects

Note: I added tests to the specs for this behavior.

@slnode

slnode commented Mar 14, 2014

Copy link
Copy Markdown

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

@bajtos

bajtos commented Mar 17, 2014

Copy link
Copy Markdown
Member

@slnode add to whitelist. ok to test.

@slnode

slnode commented Mar 17, 2014

Copy link
Copy Markdown

All is well
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/84/

@bajtos

bajtos commented Mar 17, 2014

Copy link
Copy Markdown
Member

Hi @pandaiolo, thank you for the pull request.

1. Implementation
Defining methods to export to the client via an array MyModel.prototype.clientMethods = [...] is not consistent with the way how methods are annotated in LoopBack. Instead of having a per-object array, there should be per-method property similar to shared or accepts.

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 test.e2e/test-server.js). The server provides an API for defining LoopBack models from unit tests. You should extend this API, so that tests can define custom methods with metadata.

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 }
      }
    }
  });

Comment thread lib/services.template Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) { /*...*/})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
.

@bajtos

bajtos commented Mar 17, 2014

Copy link
Copy Markdown
Member

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

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.

@ritch

ritch commented Mar 17, 2014

Copy link
Copy Markdown
Member

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:

strongloop/strong-remoting#44

Take a look at a simple example in the remoting-connector PR:

https://github.com/strongloop/loopback/tree/feature/remoting-connector/example/client-server

@bajtos

bajtos commented Mar 18, 2014

Copy link
Copy Markdown
Member

@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 sharedCode flag in the browser port, because you will split the javascript source code into three files - server, client, shared - and put all shared methods to the shared file. Is that correct?

In which case, adding sharedCode flag as a short-term temporary solution shouldn't introduce any problems later, when the browser port becomes the recommended approach. Agreed?

@seanbrookes

Copy link
Copy Markdown
Contributor

+1

On Mon, Mar 17, 2014 at 11:29 PM, Miroslav Bajtoš
notifications@github.comwrote:

@ritch https://github.com/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 sharedCode flag in the
browser port, because you will split the javascript source code into three
files - server, client, shared - and put all shared methods to the shared
file. Is that correct?

In which case, adding sharedCode flag as a short-term temporary solution
shouldn't introduce any problems later, when the browser port becomes the
recommended approach. Agreed?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/25#issuecomment-37903178
.

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*
._

@bajtos bajtos self-assigned this Mar 24, 2014
@bajtos

bajtos commented Mar 24, 2014

Copy link
Copy Markdown
Member

@pandaiolo How is the implementation going, is there anything I can help you with?

@pandaiolo

Copy link
Copy Markdown
Contributor Author

Hi, I will be able to allocate some time for it next week, this one's too
busy

2014-03-24 8:52 GMT+01:00 Miroslav Bajtoš notifications@github.com:

@pandaiolo https://github.com/pandaiolo How is the implementation
going, is there anything I can help you with?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/25#issuecomment-38418716
.

@slnode

slnode commented Apr 8, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/176/

@slnode

slnode commented Apr 8, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/177/

@slnode

slnode commented Apr 8, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback-angular/178/

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants