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

Overriding pageBeforeSend in apostrophe-pieces-pages doesn't work #1341

Closed
puntnomads opened this issue Apr 25, 2018 · 3 comments
Closed

Overriding pageBeforeSend in apostrophe-pieces-pages doesn't work #1341

puntnomads opened this issue Apr 25, 2018 · 3 comments

Comments

@puntnomads
Copy link

puntnomads commented Apr 25, 2018

I can successfully overridden pageBeforeSend to get data from the database and send it to a template. I did this in apostrophe-pages using the example in https://stackoverflow.com/questions/40011205/injecting-backend-data-into-view-for-use-in-client-side-js. However, when I try to do the same thing in apostrophe-pieces-pages, the data is not received in the template. This happens when I update the data within the callback of the database call. However, If I update the data outside of the callback of the database call, the data is received in the template.

module.exports = {
  extend: "apostrophe-pieces-pages",
  construct: function(self, options) {
    var superPageBeforeSend = self.pageBeforeSend;
    self.pageBeforeSend = function(req) {
      var pieces = req.data.pieces;
      console.log("page type: ", req.data.page.type);
      if (
        !req.data.pieces &&
        req.data.page.type.replace(/\s/g, "") === "news-page"
      ) {
        // this works
        req.data.relatedNews = "Hello World";
        return self.apos.modules["news"]
          .find(req, {})
          .sort({ title: 1 })
          .toArray(function(err, pieces) {
            if (err) {
              throw err;
            }
            // this doesn't work
            req.data.relatedNews = pieces;
            return superPageBeforeSend(req);
          });
      } else {
        return superPageBeforeSend(req);
      }
    };
  }
};
@boutell
Copy link
Member

boutell commented Apr 25, 2018

Your pageBeforeSend method must take a callback (and invoke it when it's finished, too) if it does async work.

Complicating matters, apostrophe-pieces-pages already has one which does not take a callback. That's fine but bear in mind that you'll need to take that into account when invoking superPageBeforeSend, before invoking your callback.

@boutell boutell closed this as completed Apr 25, 2018
@puntnomads
Copy link
Author

@boutell I haven't quite understood what I need to do to resolve this problem.

@boutell
Copy link
Member

boutell commented Apr 25, 2018

You're invoking things that take callbacks, and your code is meanwhile returning, so apostrophe goes its merry way and renders your page. Then, later, your data arrives. Oops, too late.

So your pageBeforeSend method must take an additional argument, callback, and must invoke it only when your work is complete.

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

2 participants