balderdashy / balderdashy/sails

Accessing locals in view

Open
#6,953 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

does this answer your question?
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Node version: v12.16.1
Sails version (sails): 1.2.3


I'm running a basic sails app, following along loosely with the Ration example from the Platzi course.

Summary of the issue:
I seem to be able to access the locals in my js file, but not in ejs.

I have a view (deck.ejs) as follows:

<div id="deck" v-cloak>
  <h1>{{deck.title}}</h1>
  <ul>
    <li v-for="entry in deck.entries">
      {{entry.term}}
    </li>
  </ul>
</div>
<%- /* Expose server-rendered data as window.SAILS_LOCALS :: */ exposeLocalsToBrowser() %>

The page file:

parasails.registerPage('deck', {
  data: {},
  beforeMount: function() {
    // Attach any initial data from the server.
    _.extend(this, SAILS_LOCALS);
  },
  mounted: async function() {
    console.log(this.deck);
  },
  methods: {}
});

The result of the console.log in the mounted function is what I expect to see:

deck: { "title": "Default", "entries": [{ "term": "test" }]

And yet, the view does not throw an error. There is simply nothing displayed on the page. I'd at least expect a reference error for deck.

I'm accessing the local with a query param.

  fn: async function (_, exits) {
    var deck  = await Deck.find({
      owner: this.req.me.id,
      id: this.req.param('id')
    }).populate('entries');

    // return the view
    return exits.success({
      deck
    });
  }

Am I doing something obviously wrong?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied action response with deck.ejs and the parasails.registerPage('deck') page script. Inspect how the returned deck local is exposed through SAILS_LOCALS and how the EJS/Vue view handles the bindings. Done means the supplied deck data is rendered in the page without silently missing output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
frontend, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.