使用PageInit事件ASP.NET MVC3 jQuery Mobile的页面的Ajax code绑定 [英] ASP.NET MVC3 jQuery mobile page's Ajax code binding using PageInit event

查看:116
本文介绍了使用PageInit事件ASP.NET MVC3 jQuery Mobile的页面的Ajax code绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写使用ASP.NET MVC3 + JqueryMobile RC1 JQueryMobile应用。
几页有我们称之为使用jQuery code自己的Ajax方法( $。的getJSON())方法。

要调用这些在Ajax调用我们做的点击里面的jQuery移动的pageinit事件事件绑定这里提到(的 http://jquerymobile.com/demos/1.0rc1/docs/api/events.html )。
但是,来电绑定到pageinit方法,通过每次访问的页面得到增加+1。例如如果我使用的后退按钮或其他任何链接再次访问我的网页,再次访问它,叫了两声pageinit方法了,里面写pageinit任何code执行两次......这些不断每次访问网页增加。

我们应该用哪个事件绑定的事件。它应该在页面加载时被调用一次?

编辑: -
我们希望默认JQM的AJAX行为,我们一直AjaxEnabled为true。

示例源$ C ​​$ C(你也可以通过创建新的MVC3的APP,并与给定的code替换下面的第三.cshtml瑞普此问题: -

我_Layout.cshtml: -

 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
    < META NAME =视口CONTENT =WIDTH =设备宽度/>
    <间的charset =UTF-8/>
    <链接rel =stylesheet属性HREF =HTTP://$c$c.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css/>
    &所述; SCRIPT SRC =HTTP://$c$c.jquery.com/jquery-1.6.4.min.js>&下; /脚本>
    <链接HREF =@ Url.Content(〜/内容/的site.css)的rel =stylesheet属性类型=文/ CSS/>
    <脚本类型=文/ JavaScript的>
        (文档)$ .bind(mobileinit功能(){
            $ .mobile.ajaxEnabled = TRUE;
        });
    < / SCRIPT>
    &所述; SCRIPT SRC =HTTP://$c$c.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js>&下; /脚本>
    @RenderSection(HeaderScripts,必需:false)
< /头>
<身体GT;
    < D​​IV数据角色=页面ID =@ ViewBag.DivTitle>
        < D​​IV数据角色=头>
            < H1>
                @ ViewBag.Title
            < / H1>
             < A HREF =/首页/数据角色=按钮数据图标=信息数据iconpos =NOTEXT>关于< / A>
              < A HREF =/首页/指数数据角色=按钮数据图标=家的数据iconpos =NOTEXT>家庭和LT; / A>
        < / DIV>        < D​​IV数据角色=内容ID =DivContent>
            @RenderBody()
            @RenderSection(BodyScriptsSection,必需:false)
        < / DIV>        < D​​IV数据角色=页脚数据位=固定>
            @RenderSection(MobileFooter,必需:false)
        < / DIV>
    < / DIV>
< /身体GT;
< / HTML>

样Index.cshtml

  @ {
    ViewBag.Title =主页;
    ViewBag.DivTitle =HomeIndex;
}@section BodyScriptsSection {
  <脚本类型=文/ JavaScript的>
      $(#@ ViewBag.DivTitle)。生活(pageshow功能(){
      警报(PageShow叫 - HomeIndex);
  });      < / SCRIPT>

样AboutUS.cshtml

  @ {
    ViewBag.Title =关于我们;
    ViewBag.DivTitle =公司简介;}@section BodyScriptsSection {
  <脚本类型=文/ JavaScript的>
      $(#@ ViewBag.DivTitle)。生活(pageshow功能(){
          警报(PageShow叫 - 关于我们);
      });      < / SCRIPT>


解决方案

你为什么不导航时离开,像这样杀了你的处理程序:

  $(#@ ViewBag.DivTitle)。生活(pagehide功能(){
    $(#@ ViewBag.DivTitle)死亡(pageshow);
}

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>

解决方案

Why don't you kill your handlers upon navigating away, something like this:

$("#@ViewBag.DivTitle").live("pagehide", function() {
    $("#@ViewBag.DivTitle").die("pageshow");
}

这篇关于使用PageInit事件ASP.NET MVC3 jQuery Mobile的页面的Ajax code绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆