2017-04-23 77 views
0

我有一些代碼如下 -訪問JavaScript中的對象文字內的函數內的函數?

var app = { 
    initialize: function() { 
     document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); 
     document.addEventListener('backbutton',this.receivedEvent.backbutton(),false); 
                ^-- //not working 
    }, 

    onDeviceReady: function() { 
     this.receivedEvent('deviceready'); 
    }, 

receivedEvent: function(id) { 
    var originalLocation, partnership; 

    function fadeOut(element, direction) { 
     .... 
    }; 

    function fadeIn(element, direction) { 
     ..... 
    } 

    function formFadeIn(direction) { 
     fadeIn($("#dataform"), direction); 
    }; 

    function formFadeOut(direction) { 
     fadeOut($("#dataform"), direction); 
    }; 

    function backbutton() { 
     var current = $("#home").attr("page-current"); 
     var prev = $("#home").attr("page-prev"); 
     if (current == "dataform" && prev == "partnertype") { 
      formFadeOut("opposite"); 
      partnertypeFadeIn("opposite"); 
      setPage("country", "partnertype") 
      $("#selectcountry").attr("disabled", false).val("AF") 
     } else if (current == "dataform" && prev == "country") { 
      formFadeOut("opposite"); 
      countryFadeIn("opposite"); 
     } else if (current == "partnertype" && prev == "country") { 
      partnertypeFadeOut("opposite"); 
      countryFadeIn("opposite"); 
     } else { 
      window.location.reload() 
     } 
    } 
} 
}; 

app.initialize(); 

所以我需要綁定內receivedEvent事件「後退按鈕」的function backbutton()function backbutton()正在調用receivedEvent內的本地函數,如formFadeIn()

我無法弄清楚綁定的確切語法。

我已經試過 -

  1. this.receivedEvent.backbutton //沒有反應

  2. this.receivedEvent.backbutton() //沒有反應

  3. this.receivedEvent.bind(this).backbutton //導致無限循環的頁面

  4. 導出backbutton()函數爲return { backbutton : backbutton } //沒有反應

我有什麼做的訪問從app.initialize()function backbutton()沒有鬆動的背景下?

回答

0

你可以嘗試:

var app = { 
    initialize: function() { 
    document.addEventListener('backbutton',this.myBackButtonFunction, false); 
    }, 

    myBackButtonFunction: function() { 
    // Your code 
    } 
    receivedEvent: function(id) { 
    // Can also be invoked from here 
    this.myBackButtonFunction(); 
    }, 

}; 


app.initialize(); 
+0

myBackButtonFunction是靠調用包含在receivedEvent等功能 - 所以很遺憾這是不行的。 – rkmr