Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote method for album? #12

Open
Sequoia opened this issue Apr 7, 2017 · 2 comments
Open

Remote method for album? #12

Sequoia opened this issue Apr 7, 2017 · 2 comments

Comments

@Sequoia
Copy link
Contributor

Sequoia commented Apr 7, 2017

I haven't actually tried it yet. . . trying to come up with something that fits the context & makes sense. I find the static "CoffeeShop.status" example confusing, as I think most people would assume as I did: that the method checks the status of a particular coffeeshop (instance), whereas in fact it just checks whether the current time is between a hardcoded range.

Instance methods require a bit more configuration (maybe?) but they are much easier to understand the purpose of IMO.

/albums/123/complete 👉 75

Anyway here it is! Alternately it could just be a "say Hi" type method, but I think for the slides if we're going with "band app" from start to finish it would be confusing to introduce a new, different model while introducing model features.

I am wondering if it wouldn't be easier (at least in the slides) to push remote methods further back, after attendees can see the basic scaffolded stuff in action, see relationships, see how far you can get without customizing.

'use strict';

module.exports = function(Album) {
/**
 * reports how complete an album record is based on not-empty fields
 * returns number between 0 and 100
 * @param {Function(Error, number)} callback
 */
  Album.prototype.percentComplete = function(callback) {
    var numFields = 5;
    var fieldsComplete = 1; // assume title is complete

    if (this.year) fieldsComplete++;
    if (this['record-label']) fieldsComplete++;
    if (this.artwork) fieldsComplete++;
    if (this.tracks.length && this.tracks.length > 0) fieldsComplete++;

    callback(null, Math.round((fieldsComplete / numFields) * 100));
  };

  Album.remoteMethod('percentComplete', {
    isStatic: false,
    description: 'Get % of album fields completed',
    http: {
      path: '/complete',
      verb: 'get',
      status: 200
    },
    returns: {root: true, type: 'number'},
  });
};
@Sequoia
Copy link
Contributor Author

Sequoia commented Apr 7, 2017

One problem with this is that it does not fit on a slide :) I just can't think of anything that relates to an album besides simply-- oh maybe a remote toString would be a reasonable example...

@Sequoia
Copy link
Contributor Author

Sequoia commented Apr 7, 2017

works. Maybe better with a wrapped response { "complete" : 80 }

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

No branches or pull requests

1 participant