weixin_33713503 2013-08-05 16:20 采纳率: 0%
浏览 22

一定有更简单的方法

I am trying to create an JQM app and are doing so by getting a lot of data from database. When I click on a link from a ajax/json generated calendar list I should then be able to get the info for that event by calling the server and get the data. As it is now I do this in 2 steps like this.

My ajax generated event list:

$.each(groupcalendar, function (i, val) {
    output += '<li><a href="#" data-id="' + val.mid + '" id="gotoMatch"><h2>' + val.matchformname + '</h2><p><strong>' + val.matchday + '</strong></p><p>' + val.coursename + '</p><p class="ui-li-aside"><strong>' + val.matchtime + '</strong></p></a></li>';
});

When I click on one of the links I want to goto a page called prematchdata.html and get the data fro that specific event. I do so by first calling the click and get the eventid from data-id like this:

$(document).on('click', '#gotoMatch', function () {

    var matchid = $(this).attr("data-id");

    $.get("http://mypage.com/json/getmatchinfo.php?matchid="+matchid, function(data) {

        localStorage["matchinfo"] = JSON.stringify(data);

        $.mobile.changePage( "prematchdata.html", { transition: "slide", changeHash: true} );

    }, "json");

});

I save the returned data as localStorage and then uses this data in my pageinit like this:

$(document).on("pageinit", "#prematchdata", function() {

    var matchinfo = {};
    matchinfo = JSON.parse(localStorage["matchinfo"])

    var content = '<h2>'+matchinfo["matchname"]+'</h2>';

    $('.infoholder').html(content);

});

It works, although for me it seems like the last 2 steps should be done in one, but i am not sure how to do so? It seems a little bit wrong get data, save locally and then use it? Can't this be done without the $(document).on('click', '#gotoMatch', function () {});?

Hoping for some help and thanks in advance :-)

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2013-08-05 20:07
    关注

    You could try sending it up using a query string. When you're using changePage, change your code like this :

    $(document).on('click', '#gotoMatch', function () {
    
        var matchid = $(this).attr("data-id");
        $.get("http://mypage.com/json/getmatchinfo.php?matchid=" + matchid, function (data) {
            paramData = data[0];
            $.mobile.changePage("prematchdata.html", {
                transition: "slide",
                changeHash: true,
                data: paramData //added this extra parameter which will pass data as a query string
            });
        }, "json");
    
    });
    

    When you're getting it back,

    $(document).on("pageinit", "#prematchdata", function() {
    
        var url = $.url(document.location);
        var name= url.param("matchname");
    
        var content = '<h2>'+ name +'</h2>';
    
        $('.infoholder').html(content);
    
    });
    

    Another easy way would be use a singlepage template instead of a multi page template. Then, you could just use a global variable to get and set data.

    That said, what you're doing right now is more secure than this query string method. By using this, anyone can see what you are sending over the URL. So I advise you keep using localStorage. For more info on this, look into this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题