1

I have a problem with executing jQuery code at EJS template. My code:

$.json('/data.json', function (data) {
    new EJS({url: 'template.ejs'}).update('mycontainer', {data: data});
})

It works. But when I've tried to use some jQuery plugin at this template - nothing. Example of my EJS-template:

<script>
   alert(0);
   $(document).ready(function () {
      $('#someId').someJQplaugin();
   })
</script>
<div id="someId"><%= data.someValue%></div>

Plugin and alert(!!) is not working. Then i replace my code between <% %> tags. Alert and document.ready is working, but jQuery can't find the object at appended EJS template.

I've tried to use jQuery out of template, but result is the same:

$('#someId') is null.

How do I use EJS and jQuery together?

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
Jim Moriarty
  • 133
  • 1
  • 4

1 Answers1

0

I solved a problem, I guess. There no callbacks at EJS on updating/inserting. I have extend EJS like this:

EJS.Prototype = {
   ...
   update: function(element, options, cb) {
   ...
   // add this to the end of function
   if(cb != null)
      cb();
   }

Now I can execute callback like this:

new EJS({url: template}).update(obj.attr('data-selector'), {data: data}, function () {
   // it works
   $('#someId').someJQplugin();
});

Hope, this will help someone )

Jim Moriarty
  • 133
  • 1
  • 4