我對於WinJS.Application.start()
函數的理解是,它允許WinJS排隊某些正常的頁面初始化事件,讓您有機會首先在您的default.js
中設置其他數據文件。通過調用default.js
末尾的start()
函數,WinJS會爲您排除所有排隊事件(例如activated
事件)。app.onactivated取決於app.start()的位置
我想了解哪裏一切都適合生命週期,所以我不清楚爲什麼下面的第一個例子工作,但第二個沒有。我做的是更新頁面標題,當我一個5秒的延遲之後調用app.start()
預期不工作:
首先,這裏default.html
:
<html>
<head>
<script references...>
</head>
<body>
<h1 id="pageTitle">Page of coolness...</h1>
</body>
</html>
而這裏的第一default.js
例子(按預期工作):
(function() {
var app = WinJS.Application;
app.onactivated = function() {
document.getElementById("pageTitle").innerText = "Rock it!";
};
// This code *does* fire the onactivated event:
// The page displays "Rock it!" in the page title
app.start();
})();
這裏是第二default.js
例子(如預期不工作):
(function() {
var app = WinJS.Application;
app.onactivated = function() {
document.getElementById("pageTitle").innerText = "Rock it!";
};
// This code *doesn't* fire the onactivated event:
// It initially displays "Page of coolness..." in the page title.
// After 5 seconds, it adds "Gettin' there...Almost made it..."
// to the the page title, but "Rock it!" never gets displayed
WinJS.Promise.timeout(5000).then(function() {
document.getElementById("pageTitle").innerText += "Gettin' there...";
app.start();
document.getElementById("pageTitle").innerText += "Almost made it...";
});
})();
爲什麼在5秒後調用app.start()
導致activated
事件不火?
不是一個答案,但裝載的確實火。您可以在調試器中遍歷base.js,並且會看到執行的差異序列。這幾乎就好像激活的事件處理程序被「連接太遲」一樣,所以當它觸發時,它還沒有被連接,並且是空的,但是不能解釋爲什麼或者如果這是合適的行爲。 –
@Jim _「就好像被激活的事件處理程序被連接'太晚'一樣,所以當它被觸發時,它還沒有被連接並且是空的......」_...事實證明,這正是發生了,它看起來像是按照設計的方式(儘管我不知道爲什麼) – RSW