Question

Has anyone managed to get the auto-load functionality of express-resource to work? I cannot get it to fire, but according to the documentation it should work: express-resource

I have tried getting it to work in both my own code and the example code in express-resource provided here. I am simply adding a "console.log" statement into the forum resource's "load" function, however it seems to never be called. Code is as follows:

exports.load = function(id, fn){
  console.log('auto-loader');
  process.nextTick(function(){
    console.log('auto-loader in the callback');
    fn(null, { title: 'Ferrets' });
  });
};

Then in the "controller.js" the code is exactly as in the example here:

var forums = app.resource('forums', require('./controllers/forum'));

The reason I would like to get this working is that I want to have a blanket function that loads some data for a particular set of routes, as opposed to adding it into each route individually as middleware. E.g. I have "listings" on my site, and I want a function that retrieves the top 10 recent listings.

Any ideas would be greatly appreciated I spent much of last night puzzling and concluding that the code may not work at all. I sincerely hope I'm wrong and hope to stand corrected by one of you smart cookies, because I'd really like it if I did not have to pick a different routing module!

Alternatively if you have a routing module that works with express that you know has functioning auto-load capability then please also let me know?

Was it helpful?

Solution

Alright, in case anyone else runs into this - I have found that I'm incorrect and it does actually fire, but only sometimes!!

The problem is that it does not fire on all routes. So far I have tested index, new, show, and edit. It fires for show and edit but not for index or new.

I am not sure if this is intentional or not and would love it if someone could shed some light on why it behaves like this please?

I have also left a comment on a very similar question on the author's github site: load function doesn't add to req object on index route

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top