Search code examples
javascriptjquerymeteormeteor-helper

Meteor onRendered Unexpected token =


Am having an issue of using JQuery plugin in meteor template. I tried this plugin.

 <div id="listId">
  <ul class="list">
      // A bunch of items
  </ul>
  <ul class="pagination"></ul>
</div>

<script>
  var options = {
    valueNames: [ 'name', 'category' ],
    page: 3,
    plugins: [
      ListPagination({})
    ]
  };

  var listObj = new List('listId', options);
</script>

I put about javascript code in onRendered.

Template.MyTemplate.onRendered({
    listObj = new List('listId', {
      valueNames: [ 'name', 'category' ],
      page: 3,
      plugins: [
        ListPagination({})
      ]
    });`enter code here`
});

But I am getting error.

MyTemplate.js:2:13: Unexpected token =

Solution

  • You are passing object ({}) to onRendered function:

    Template.MyTemplate.onRendered({
        listObj = new List('listId', {
          valueNames: [ 'name', 'category' ],
          page: 3,
          plugins: [
            ListPagination({})
          ]
        });
    });
    

    You should pass function:

    Template.MyTemplate.onRendered(function() {
        listObj = new List('listId', {
          valueNames: [ 'name', 'category' ],
          page: 3,
          plugins: [
            ListPagination({})
          ]
        });
    });