Reputation: 351
In my project we are using snap view ,the issue is if we use the snap view in homepage.js it is effecting on all pages
window.addEventListener("resize", onResize,false);
this code is calling on all pages where we snap.
(function () {
"use strict";
var pageName = "homePage";
WinJS.UI.Pages.define("/pages/home/home.html", {
ready: function (element, options) {
window.addEventListener("resize", onResize,false);
}
function onResize() {
var pageName = "homePage";
if (Windows.UI.ViewManagement.ApplicationView.value == Windows.UI.ViewManagement.ApplicationViewState.fullScreenLandscape) {
var UsrName = localStorage.getItem("username");
var parameters = "'strUserName':'" + UsrName + "'";
ServiceCall("GetAllDeals", parameters, pageName);
var UsrName = localStorage.getItem("username");
var UId = localStorage.getItem("UserID");
var parameters = "'strUserName':'" + UsrName + "','UserID':'" + UId + "'";
ServiceCall("TrackDeals", parameters, pageName);
}
}
this is the code we use in home page ,but when we are in some other page like trackpage.js and when we snap that page and unsnap we are going back to homepage.js onResize function service call.how to avoid this,and implement only on current page .please let me know if any error in this code.
Upvotes: 1
Views: 386
Reputation: 5633
Because the onResize function is added to the window object, you must remove it if you do not want it to be called when the window changes size. Even though you may navigate away from home.html, the event is still connected. That's just the way SPA-style apps work, including modern Win8 apps.
To solve this, you have two choices, you can handle it before navigation occurs with:
WinJS.Navigation.onbeforenavigate = function () { window.removeEventListener('resize', onresize);};
or you can handle it with the WinJS Page unload function:
WinJS.UI.Pages.define("/pages/home/home.html", {
unload: function() {window.removeEventListener('resize', onresize);},
// rest of Page code, including Ready goes here
}
which ever one works best for you. onbeforenavigate fires before unload if that is ever important to you.
Upvotes: 2