concept local in category sails

appears as: locals, locals, The locals
Sails.js in Action

This is an excerpt from Manning's book Sails.js in Action.

Figure 8.8. An incoming GET request to / B matches an explicit route and passes a locals dictionary c to a view named homepage.ejs d. The view contains a special tag that, when rendered, inserts a value from the locals dictionary as a response e to the requesting user-agent.

This allows you to share the same layout, partials, and (to some degree) locals, across each of your different views. Currently, your videos.ejs view is served by an explicit route that simulates an unauthenticated state of a dummy user. And each of the other views has a corresponding custom route in brushfire/config/routes.js that simulates an authenticated state, similar to the following listing.

Example 8.9. locals used to simulate the authenticated state of a user
...
locals: {
      me: {
        id: 1,
        gravatarURL:
         'http://www.gravatar.com/avatar/ef3eac6c71fdf24b13db12d8ff8d1264?',
        email: 'sailsinaction@gmail.com'
      }
    }
...

8.2.6. Hardcoding locals in a route

You’ve seen how Sails makes data available to views with variables called locals. In chapter 9, you’ll transition to setting locals in your controller actions. For now, just so you get to see some action, you’ll pass through stub data in your explicit route. In Sublime, open brushfire/config/routes.js, and add the following locals to the GET / route.

Example 8.8. Adding locals to a view in your custom route

Here, you pass a static dictionary of a simulated user to the view via locals. Figure 8.14 illustrates the compiling process where locals passed in the route are injected into a view to produce your server-rendered view.

Figure 8.14. Using locals through a custom route to bootstrap data in a server-rendered view

The route triggers the view along with locals. You use template tags that will stringify the value of the me dictionary or insert null if the me local is empty . The result of this compile is a server-rendered view that contains the me dictionary . The me dictionary will inform the frontend whether the user who made the request is authenticated or unauthenticated. Let’s see this in action. Restart Sails using sails lift, and navigate your browser to localhost:1337. Your browser should look similar to figure 8.15.