We are writing JQueryMobile application using ASP.NET MVC3 + JqueryMobile RC1.
Few pages have their own Ajax methods which we call using jQuery code ($.getJSON()
) method.
To invoke these in Ajax calls we do click event bindings inside "pageinit" event of jquery-mobile as mentioned here(http://jquerymobile.com/demos/1.0rc1/docs/api/events.html).
But, call to method binded to pageinit get increased +1 by each visit to the page. e.g. if i visit again to my page using back button or from any other link, visit it again, pageinit method called twice, and any code written inside pageinit executed two times...these keep on increasing with each visit to page.
Which event should we use to bind events. and it should be called only once on page load?
Edit:-
We want default AJAX behavior of JQM and we have kept AjaxEnabled to true.
Sample source Code(you may repro this issue by creating new MVC3 APP and replacing below three .cshtml with given code:-
My _Layout.cshtml:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).bind("mobileinit", function () {
$.mobile.ajaxEnabled = true;
});
</script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
@RenderSection("HeaderScripts", required: false)
</head>
<body>
<div data-role="page" id="@ViewBag.DivTitle">
<div data-role="header">
<h1>
@ViewBag.Title
</h1>
<a href="/Home/About" data-role="Button" data-icon="info" data-iconpos="notext">About</a>
<a href="/Home/Index" data-role="Button" data-icon="home" data-iconpos="notext">Home</a>
</div>
<div data-role="content" id="DivContent">
@RenderBody()
@RenderSection("BodyScriptsSection", required: false)
</div>
<div data-role="footer" data-position="fixed">
@RenderSection("MobileFooter", required: false)
</div>
</div>
</body>
</html>
Sample Index.cshtml
@{
ViewBag.Title = "Home Page";
ViewBag.DivTitle = "HomeIndex";
}
@section BodyScriptsSection {
<script type="text/javascript">
$("#@ViewBag.DivTitle").live("pageshow", function () {
alert ("PageShow Called - HomeIndex");
});
</script>
Sample AboutUS.cshtml
@{
ViewBag.Title = "About Us";
ViewBag.DivTitle = "AboutUS";
}
@section BodyScriptsSection {
<script type="text/javascript">
$("#@ViewBag.DivTitle").live("pageshow", function () {
alert("PageShow Called - AboutUS");
});
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…