0

I have a javascript code

 Html5Template_300x250 = function(config) {
      this.config = config;
      var self = this;
      adkit.onReady(this.init());

  };
  Html5Template_300x250.prototype = {
      // Function That Creates Element Var
      d: function(id) {
          return document.getElementById(id);
      },

      // Initialize DCO HTML5 template
      init: function() {

          adkit.onReady(this.handleSVData);

      },
      handleSVData: function() {
          var myData = adkit.getSVData("varName");

          alert(myData);

          this.startAd();

      },

      startAd: function(data) {
          alert("test2");

      }
  }

In the above code i have used an external javascript adkit.js and using that method in my code. The initial method is started as

 adkit.onReady(this.init());

It is calling a init function and which is then calling other methods including handleSVData which is getting a value from the json file which is in the root folder as

var myData = adkit.getSVData("varName");

The part of the code is working fine but after that line i am calling another method

this.startAd();

But this method is not working and i am getting error

TypeError: this.startAd is not a function

I am not good in javascript and giving me headaches can someone explain me why it is so complicated and what i am doing wrong here ??

user3754380
  • 651
  • 2
  • 7
  • 15
  • 1
    This doesn't look right `adkit.onReady(this.init())`. Then, what you want is `adkit.onReady(this.handleSVData.bind(this))` – elclanrs Aug 15 '14 at 00:36
  • well this part is working fine and it is under the specification to use the adkit like this to initialize the init method and adkit.getSVData to get values from the JSON file and this part is completely working fine but this.startAd() is not working – user3754380 Aug 15 '14 at 00:38
  • Learn about how `this` works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this. Also see http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback – Felix Kling Aug 15 '14 at 00:38
  • 1
    Still, doesn't look right, because `onReady` expects a function I suppose, and `this.init` doesn't return a function, you probably want to pass a reference to it, not execute it. – elclanrs Aug 15 '14 at 00:38
  • this is how it is explain adkit.onReady(CallbackFunctionName);Waits for AdKit initialization and runs a callback function. – user3754380 Aug 15 '14 at 00:40
  • this.startAd(); works fine in other normal method but not in the handleSVData. This is because it goes out of the scope and then callback doesnot find this function? – user3754380 Aug 15 '14 at 00:43
  • @elclanrs - his `adkit.onReady(this.init());` is, of course, wrong, because `init` does not return anything. What matters is the `init()` call itself, that passes `this.handleSVData` to `adkit.onReady`. He could just call `this.init();` instead. – Igor Aug 15 '14 at 00:49
  • @lgor what do you mean . Can you explain me what i am doing wrong here? – user3754380 Aug 15 '14 at 00:51
  • Should it be adkit.onReady(this.init()); or adkit.onReady(this.init); without the brackets so that you are passing the function reference? In fact couldn't it be rewitten as adkit.onReady(this.handleSVData);? – paul Aug 15 '14 at 00:53

1 Answers1

0

When handleSVData is called by adkit it is called in the scope that is not an instance of Html5Template_300x250 - that is why this does not have startAd method.

As for adkit.onReady(this.init()); line.

adkit.onReady expects a function as a parameter. It stores this function variable and calls when it is time for onReady event. This is set correctly by adkit.onReady(this.handleSVData); line. this.init(), however, is a call to init function and your line adkit.onReady(this.init()); passes to adkit.onReady whatever init returns. But it does not return anything - you are passing undefined as parameter.

  init: function() {
      var template = this;
      adkit.onReady(function(){
        template.handleSVData();
      });
  },

And change line

adkit.onReady(this.init());

to

this.init();
Igor
  • 15,833
  • 1
  • 27
  • 32
  • then how to fix this what if i want to call startAd after that" – user3754380 Aug 15 '14 at 00:56
  • adkit.onReady(this.handleSVData.bind(this)) this has fixed everything but not sure how? – user3754380 Aug 15 '14 at 01:06
  • consider the `bind` method of a javascript function a short-hand notation of the `init` implementation that I showed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind – Igor Aug 15 '14 at 01:15