+1 vote
576 views
in Programming by (1.9k points)
edited by
Want to store current context to a variable using this code , can any one help me what I am missing here? 
Because  in handling event, getting $this as this of window object.
 
var Modal = (function() {
 
    $this = this;
    var el = '.modal';
 
    return {
        init: function() {
            $(window).on('showModal', function(e, data) {
                $this.getData(data.controller, data.action, data.id);
            });
        },
        getData: function(controller, action, id) {
            var html = $.ajax({
                url: '/' + controller + '/' + action + '/' + id ? id : '',
                async: false
            }).responseText;
            this.render(html);
        },
        render: function(html) {
            $(el).html(html);
            $(el).modal();
 
            this.event();
        },
        event: function() {
 
        }
    };
})();
Thanks For Help!

1 Answer

+2 votes
by Expert (6.4k points)

I think you should try like this see the code here :

var Modal = (function() {
 
  var el = '.modal';
 
  function init() {
    $(window).on('showModal', function(e, data) {
      getData(data.controller, data.action, data.id);
    });        
  }
 
  function getData(controller, action, id) {
    var html = $.ajax({
      url: '/' + controller + '/' + action + '/' + id ? id : '',
      async: false
    }).responseText;
    render(html);
  }
 
  function render(html) {
    $(el).html(html);
    $(el).modal();
    event();
  }
 
  function event() {
 
  }
 
  return {init:init, getData:getData, render:render, event:event}
})();
I hope this will help you.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated