Sunday, 4 June 2017

Node - EJS - Display toastjs message

I'm trying to display a toast message using the library ToastrJS. The message display okay. The problem is that I'm not finding a way to pass the error message. I use connect-flash to do flash-messages, and then I wanna pass the message to the toaster.

My EJS code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><%=title%></title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/stylesheets/nav-bar.css">
    <link rel="stylesheet" href="<%=style%>">
    <link rel="stylesheet" href="/js/aos.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/css/toastr.css" rel="stylesheet"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.0.1/js/toastr.js"></script>
  </head>
  <body >
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Joobber</a>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav">
            <li class=""><a href="#">Home</a></li>
            <li><a href="#">Ver todas as vagas</a></li>
            <li><a href="#">Vagas por categoria:</a></li>

          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"><span class="glyphicon glyphicon-user"></span> Ainda não está cadastrado?</a></li>
            <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Logar</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <% if(success.length > 0){ %>
      <script>
        toastr.success('WANNA USE THE ERROR MESSAGE VARIABLE HERE!!!!')
      </script>
    <% } %>

    <script type="text/javascript" src="js/aos.js">

    </script>
    <script type="text/javascript">
    AOS.init({
      offset: 200,
      duration: 600,
      easing: 'ease-in-sine',
      delay: 100,
      });
    </script>

I also have connect-flash configured and used some locals variables to set the specific kind of message

app.use(flash());
var sessionFlash = function(req, res, next) {
    res.locals.currentUser = req.user;
    res.locals.error = req.flash('error');
    res.locals.success = req.flash('success');
    next();

}
app.use(sessionFlash)

And then, my routes:

const express = require('express')
const router = express.Router()
const indexControllers = require('../controllers/indexControllers')


router.get('/', indexControllers.HomePage)

router.get('/signup', indexControllers.Signup)

router.get('/test', (req, res)=>{
  req.flash('success', 'user succesfulyl registered')
  res.redirect('/')
})

module.exports = router;



via Gustavo Dias

No comments:

Post a Comment