Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js - passing data to a view

I've recently started learning how to use Sails.js and I came across a small issue.

I have a model called Applicant, as follows:

module.exports = {

  attributes: {     
     name: {
      type: 'STRING',
      required: true
     },

     email: {
      type: 'STRING',
      required: true
     }

  }

};

I have built the index action to return all the instances of the Applicant model:

module.exports = {
    index: function(req, res) {
        Applicant.findAll().done(function(err, applicants) {
            res.view({
                apps: applicants
            });
        });
    }
};

And this is the view that I am using:

<section id="applicants">
    <h1>List of applicants</h1>

    <ul>
        <% for (applicant in apps) { %>
        <li><%= applicant.name %> <%= applicant.email %></li>
        <% } %>
    </ul>
</section>

Still, when I load the corresponding URL, I can't see my data displayed.

I have read the Sails.js documentation, but I can't seem to find what's wrong with my code.

It would be great if someone could help me solve this problem.

Thank you!

like image 388
vladzam Avatar asked Aug 14 '13 10:08

vladzam


People also ask

What is waterline in sails JS?

Waterline is a next-generation storage and retrieval engine, and the default ORM used in the Sails framework.

How does sails JS work?

Sails. js uses Grunt as a build tool for building front-end assets. If you're building an app for the browser, you're in luck. Sails ships with Grunt — which means your entire front-end asset workflow is completely customizable, and comes with support for all of the great Grunt modules which are already out there.

What is res view?

res. view() reads a view file from disk, compiles it into HTML, then streams it back to the client. If you already have the view in memory, or don't want to stream the compiled HTML directly back to the client, use sails.

Is sails JS framework?

js (or Sails) is a model–view–controller (MVC) web application framework developed atop the Node. js environment, released as free and open-source software under the MIT License. It is designed to make it easy to build custom, enterprise-grade Node. js web applications and APIs.


1 Answers

Try

<%= apps[applicant].name %>

It's just like for loop works. applicant is an index in apps array.

Edit: Better way is to use javascript array's built in method forEach, because this way you avoid cycling through elements, inherited from apps's type (for example, elements, assigned like this: apps.__proto__.foo = {name: "bar"} or Array.prototype.foo2 = {name: "pi222"}).

<% apps.forEach(function(applicant)  { %>
    <li><%= applicant.name %> <%= applicant.email %></li>
<% }); %>

P.S. Of course this javascript's method (forEach) wouldn't work in IE <= 8(if you consider to use it in browser). But we are inside NodeJs, which is built on top of V8 engine. So this will work, anyway.

like image 163
ataman Avatar answered Oct 02 '22 19:10

ataman