scope ... createDelegate

scope ... createDelegate

I'm having a problem with scope. Below is the code I am using, and I get the following error:

TypeError: Error #2007: Parameter listener must be non-null.

With ExtJS I could do this in the init function and the scope would be correct.

init: function() {
    window.nativeWindow.addEventListener(air.Event.CLOSING, this.windowClosingHandler.createDelegate(this));
    this.prefsFile = air.File.applicationStorageDirectory;
    this.prefsFile = this.prefsFile.resolvePath("preferences.xml");
    this.readXML();
},


So how can I achieve this with jQuery?

$(document).ready(function() {
   MyApp.init();
});

var MyApp = {
   prefsFile: '',
   prefsXML: '',
   stream: '',
   
   init: function() {
      window.nativeWindow.addEventListener(air.Event.CLOSING, this.self.windowClosingHandler);
      this.prefsFile = air.File.applicationStorageDirectory;
      this.prefsFile = this.prefsFile.resolvePath("preferences.xml");
      this.readXML();
   },
   
   readXML: function() {
      this.stream = new air.FileStream();
      
      if (this.prefsFile.exists) {
         this.stream.open(this.prefsFile, air.FileMode.READ);
         this.processXMLData();
      } else {
         this.saveData();
      }
   },
   
   processXMLData: function() {
      this.prefsXML = this.stream.readUTFBytes(this.stream.bytesAvailable);
      this.stream.close();
      var domParser = new DOMParser();
      this.prefsXML = domParser.parseFromString(this.prefsXML, "text/xml");
      var windowState = this.prefsXML.getElementsByTagName("windowState")[0];
      window.moveTo(windowState.getAttribute("x"), windowState.getAttribute("y"));
   },
   
   saveData: function() {
      this.createXMLData();
      this.writeXMLData();
   },
   
   createXMLData: function() {
      var cr = air.File.lineEnding;
         
      this.prefsXML =   "<?xml version='1.0' encoding='utf-8'?>" + cr
         + "<preferences>" + cr
         + "   <windowState" + cr
         + "      x = '" + window.screenLeft.toString() + "'" + cr
         + "      y = '" + window.screenTop.toString() + "'" + "/>" + cr
         + "   <saveDate>" + new Date().toString() + "</saveDate>" + cr
         + "</preferences>";
   },
   
   writeXMLData: function() {
      this.stream = new air.FileStream();
      this.stream.open(this.prefsFile, air.FileMode.WRITE);
      this.stream.writeUTFBytes(this.prefsXML);
      this.stream.close();
   },
   
   windowClosingHandler: function(e) {
      e.preventDefault();
      window.nativeWindow.removeEventListener(air.Event.CLOSING, this.self.windowClosingHandler);
      this.saveData();
      air.NativeApplication.nativeApplication.exit();
   }
};