Hey there! any question in your mind? Ask It Now!.

Popular Categories





How to store current context to local variable using JavaScript

+1 vote
243 views
asked in Programming by Eathen (1,930 points)
edited by Bhartesh
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
answered by krish Expert (6,310 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.

Related Questions

+1 vote
1 answer 145 views
+1 vote
2 answers 156 views
+1 vote
1 answer 75 views
+5 votes
2 answers 1,135 views
0 votes
1 answer 98 views
+1 vote
1 answer 41 views
+4 votes
5 answers 2,075 views
+1 vote
1 answer 730 views
+6 votes
1 answer 1,568 views

Not a Member yet?

Ask to Folks Login

My Account
702 Folks are online
48 members and 654 guest online
Your feedback is highly appreciated