em-wilson / book-node-mongodb-backbone

Example source code accompanying O'Reilly's "Building Node Applications with MongoDB and Backbone" by Mike Wilson

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about chapter 6: reset password

bethrobson opened this issue · comments

In the reset password jade template code, can you explain more about how "#{locals.accountId}" works?

In the paragraph after the reset success jade template, you say we could have implemented these views as client code, but you chose to do it on the server because they are so important. Can you explain more about this choice? How do you know which is the right choice for any code?

If you take a look at the route that calls the resetPassword.jade in the app.js, you'll see it called with the following:

res.render('resetPassword.jade', {locals:{accountId:accountId}});

This is creating an object named locals, with a single member: accountId (accessible within the jade template as locals.accountId, conveniently enough).

And yes, the reset password and successful reset could have been just as easily been implemented as a Backbone View, working exactly the same as all the rest of this chapters views (a Backbone View renders an HTML template, fills it with model data fetched from Node) -- but the point isn't that they're important, it's that they're functionally different and discrete. It helps provide differentiation between standard day-to-day functionality (implemented with Backbone) and one-off disaster recovery stuff (using Jade templates). At least, it seems to me to be the point.

While running this code with Express 3.0.6 an error occurred: accountId was not being passed by res.render().
I then founf this Q&A (http://stackoverflow.com/questions/10199400/expressjade-local-variable-not-available-in-view/10205586#10205586) that stated 'locals:' is no longer used in res.render().
The code that worked for me was:
res.render('resetPassword.jade', {accountId:accountId});