【问题标题】:append new records with already loaded record on webpage在网页上附加已加载记录的新记录
【发布时间】:2015-02-08 00:03:52
【问题描述】:

我正在使用 getjson 在页面上显示记录。基本上我只想在网页上显示已加载记录的新添加记录。我使用了设置间隔选项,它的工作正常问题是当设置间隔调用它时,它会附加所有显示重复的记录。请告诉我如何处理这种重复。

 function updates() {
var url="http://192.168.0.102/newsget.php";

$.getJSON(url,function(json){


// loop through the members here
$.each(json.members,function(i,dat){
$("body").append(

'<div data-role="page" id="'+dat.pagename+'" data-title="'+dat.pagename+'">'+
        '<div data-role="header">'+
            '<h1>'+dat.post_title+'</h1>'+
            '<a data-role="button" href="#news" data-transition="flip" data-icon="arrow-l" data-corners="false">Back</a>'+
        '</div>'+
        '<div data-role="content">'+

            '<img src="http://192.168.0.102/uploads/'+dat.imageurl+'" alt="'+dat.post_title+'" />'+

            '<p>'+dat.description+'</p>'+

        '</div>'+
        '<div data-role="footer">'+
            '<h2>Copyrights All rights Reserved by ABC</h2>'+
            '<p class="copyright">&copy; Copyright 2015</p>'+
        '</div>'+
    '</div>'




    );
   setTimeout(updates, 2000);

   });




   $.each(json.members,function(i,dat){
   $("#grid").append(

   '<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="false" data-iconpos="right" data-theme="c" class="portfolio-item ui-btn ui-btn-icon-right ui-li ui-li-has-alt ui-li-has-thumb ui-first-child ui-btn-up-c ui-corner-none"><div class="ui-btn-inner ui-li ui-li-has-alt">'+
                    '<div class="ui-btn-text">'+
                        '<a href="#'+dat.pagename+'" class="ui-link-inherit ui-corner-none">'+
                            '<img src="http://192.168.0.102/uploads/'+dat.imageurl+'" class="ui-li-thumb ui-corner-none">'+
                            '<h3 class="ui-li-heading">'+dat.post_title+'</h3>'+
                            '<p class="ui-li-desc">'+dat.post_content+'</p>'+
                        '</a>'+
                    '</div>'+
                '</div>'+
                '<a href="#portfolio1" title="Purchase album" class="ui-li-link-alt ui-btn ui-btn-icon-notext ui-btn-up-c" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="false" data-iconpos="notext" data-theme="c">'+
     '<span class="ui-btn-inner"><span class="ui-btn-text">'+
     '</span>'+
     '<span data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="arrow-r" data-iconpos="notext" data-theme="b" title="" class="ui-btn ui-btn-up-d ui-shadow ui-btn-corner-all ui-btn-icon-notext"><span class="ui-btn-inner"><span class="ui-btn-text"></span><span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></span></span></span></a>'+



                '</li>'




        );


       });


       })
      .done(function() {
      //alert( "second success" );
      })
      .fail(function() {
      alert( "Check your net connection or server is down due to some maintence" );
      });

      }

     });

【问题讨论】:

  • 据我所知,您没有模型。看起来您每隔 n 秒检查一次数据,并且您想用新数据更新您的视图。您对创建模型并将其用作您视图中的内容的上下文感到满意吗?
  • 基本上,总而言之,为您的模型创建一个变量(看起来像一个数组)。那么 getJSON 函数的回调应该指向一个更新模型的函数。然后在更新模型后,您可以再次渲染您的内容。 (分离代码来渲染你的视图,只担心渲染模型数据)我个人会使用把手或一些JST而不是字符串连接。
  • 您好 Charlie,首先我要感谢您提供此代码。基本上我正在创建 jquery 移动应用程序。在我的应用程序有“新闻”部分,我正在从基于 php 的管理面板添加新闻,除了用户已经打开该新闻页面的唯一问题,当时我添加了一个新闻,所以我想显示该消息会自动在用户移动设备上不刷新页面。我希望你能理解,所以如果你有任何好的东西或建议或代码,请与我分享。如果您需要更多说明,请告诉我。我也会使用你的代码
  • 是的,我明白了。老实说,我会为此使用网络套接字。你有时间可以研究一下。无论如何,你要做的就是所谓的长轮询,我的回答会很好,因为它只会向页面添加新记录。您只需要使用 Ajax 并轮询您的服务器(您的 setTimeout),然后将响应发送到 Model.getNewData
  • 查理我也需要你的帮助,你能告诉我如何保护我的 .apk 文件或如何隐藏或加密页面上的 javascript 代码。我正在使用科尔多瓦

标签: javascript php jquery jquery-mobile


【解决方案1】:

假设预加载的 div 已经像新生成的一样分配了 id,在 $("body").append 部分之前,您可以放置​​一个 if 条件以检查当前 div 是否已经存在。像这样的:

function updates() {
  var url="http://192.168.0.102/newsget.php";

  $.getJSON(url,function(json){

  // loop through the members here
  $.each(json.members,function(i,dat){
    if ($("#"+dat.pagename).length === 0) { // New if condition!
      $("body").append(...

有点快'n脏,但我希望它有所帮助。

PS:我建议你正确缩进你的代码!

【讨论】:

    【解决方案2】:
    // My Model
    var Model = {
    
        url: 'your/url/here.php',
    
        content: [{
            id: 9087,
            foo: 'foo'
        }, {
            id: 9088,
            foo: 'foo'
        }],
    
        getNewData: function () {
            $.getJSON(this.url, this.addNewRecords);
        },
    
        addNewRecords: function (arr) {
            // important to distinguish JSON from JSOL
            var i = 0;
    
            for (i; i < arr.length; i += 1) {
                if (!this.keyValueInModel('id', arr[i].id)) {
                    Model.content.push(arr[i]);  // push the record into your model
                    myView.render([arr[i]]); // render the record to the view
                }
            }
    
        },
    
        keyValueInModel: function (key, val) {
            var i = 0,
                result = false,
                arr = Model.content;
    
            for (i; i < arr.length; i += 1) {
                if (arr[i][key] === val) {
                    return true;
                }
            }
    
            return false;
        }
    };
    
    
    // My View
    var myView = {
    
        context: null, // +1 if you figure out why you should context your view to a specific node, and actually do it. I figure I will leave you some room to learn....
    
    
    
        render: function (records) {
    
            var i = 0,
                dat;
    
            for (i; i < records.length; i += 1) {
                dat = records[i];
    
                $("body").append(
    
                    '<div data-role="page" id="' + dat.pagename + '" data-title="' + dat.pagename + '">' +
                    '<div data-role="header">' +
                    '<h1>' + dat.post_title + '</h1>' +
                    '<a data-role="button" href="#news" data-transition="flip" data-icon="arrow-l" data-corners="false">Back</a>' +
                    '</div>' +
                    '<div data-role="content">' +
    
                    '<img src="http://192.168.0.102/uploads/' + dat.imageurl + '" alt="' + dat.post_title + '" />' +
    
                    '<p>' + dat.description + '</p>' +
    
                    '</div>' +
                    '<div data-role="footer">' +
                    '<h2>Copyrights All rights Reserved by ABC</h2>' +
                    '<p class="copyright">&copy; Copyright 2015</p>' +
                    '</div>' +
                    '</div>');
    
            }
    
            $("#grid").append(
    
                '<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="false" data-iconpos="right" data-theme="c" class="portfolio-item ui-btn ui-btn-icon-right ui-li ui-li-has-alt ui-li-has-thumb ui-first-child ui-btn-up-c ui-corner-none"><div class="ui-btn-inner ui-li ui-li-has-alt">' +
                '<div class="ui-btn-text">' +
                '<a href="#' + dat.pagename + '" class="ui-link-inherit ui-corner-none">' +
                '<img src="http://192.168.0.102/uploads/' + dat.imageurl + '" class="ui-li-thumb ui-corner-none">' +
                '<h3 class="ui-li-heading">' + dat.post_title + '</h3>' +
                '<p class="ui-li-desc">' + dat.post_content + '</p>' +
                '</a>' +
                '</div>' +
                '</div>' +
                '<a href="#portfolio1" title="Purchase album" class="ui-li-link-alt ui-btn ui-btn-icon-notext ui-btn-up-c" data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="false" data-iconpos="notext" data-theme="c">' +
                '<span class="ui-btn-inner"><span class="ui-btn-text">' +
                '</span>' +
                '<span data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="arrow-r" data-iconpos="notext" data-theme="b" title="" class="ui-btn ui-btn-up-d ui-shadow ui-btn-corner-all ui-btn-icon-notext"><span class="ui-btn-inner"><span class="ui-btn-text"></span><span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></span></span></span></a>' +
    
    
    
                '</li>'
    
    
    
    
            );
        }
    };
    
    setTimeout(Model.getNewData, 2000);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-12
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      相关资源
      最近更新 更多