Advertisement
nubilfi

DOM socket.io data

Jan 12th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. routes/index.js
  2. =====================
  3.  
  4. /* GET home page. */
  5.     router.get('/', function(req, res, next) {
  6.         var messages = req.flash('error');
  7.         Item.find({}, {'item_name':1, 'stock':1}, function(err, docs) {
  8.             res.render('index', { title: 'Express', csrfToken: req.csrfToken(), messages: messages, loginErrors: messages.length > 0});
  9.             io.on('connection', function (socket) {
  10.                 socket.emit('items', { item: docs });
  11.             });
  12.         });
  13.     });
  14.  
  15. views/index.hbs
  16. ==================
  17. <div class="list-group list-item-status" id="listItem">
  18.      {{#each items }}
  19.      <li class="list-group-item" id="listGroup">
  20.          {{#each this }}
  21.                {{ this.item_name }}
  22.                <span class="badge">{{ this.stock }}</span>
  23.          {{/each}}
  24.      </li>
  25.      {{/each}}
  26. </div>
  27.  
  28. try to use DOM to become like above code
  29. =========================================
  30. <script src="/socket.io/socket.io.js" type="text/javascript"></script>
  31. <script>
  32.     var socket = io();
  33.         socket.on('items', function (data) {
  34.             var docs = JSON.stringify(data, null, 4);
  35.  
  36.             $.each(JSON.parse(docs), function (index, val) {
  37.                 console.log(val);
  38.                 for (var i = 0; i < val.length; i++) {
  39.                     $('#listItem').html($('<li class="list-group-item" id="listGroup">'));
  40.                 }
  41.             });
  42.         });
  43. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement