weixin_33737134 2013-04-08 20:39 采纳率: 0%
浏览 26

PHP,Ajax,JSON和把手

If this is a redundant question please let me know and point me there, but I am not finding the exact question/answer combination nor a mixture of the two.

I have some ajax in jquery that calls a function from a php page. Without the handlebars the json data comes back nice and pretty. No issues there. However I can't seem to get ajax to write out my results to a handlebars template. Here is the code I have now:

$.ajax({
type: "post",
    url: "../includes/db_functions.inc.php",
    data: ({ p : p, p2 : p2, f : f }),
    dataType: "json",
    success: function(results){
$.each(results, function(i, item){
     var context = [ 
     {
            id : item[i].id, 
            clock_number : item[i].clock_number,
     }
    ],
    template = Handlebars.compile( $('#template').html() );
    $('table.entries').append( template(context) );
});
}
});

anyone know what I might be missing here? I 99% sure its in the context area, but just not finding it.

[edit]

<table class="entries">
    <script id="template" type="text/x-handlebars-template">
            <tr>
                <td>{{id}}</td>
                <td>{{clock_number}}</td>
            </tr>
    </script>
</table>
  • 写回答

2条回答 默认 最新

  • weixin_33686714 2013-04-08 20:55
    关注

    From Handlebars - Getting Started, I would say, leave out the array and use just the object as context

    var context = {
        id : item[i].id, 
        clock_number : item[i].clock_number,
    },
    template = Handlebars.compile($('#template').html());
    $('table.entries').append(template(context));
    

    In the ajax success code, there's the call to $.each(results, function(i, item) {...}). In this context, item is already the ith element, if results is an array. Maybe you can reduce this to

    success: function(results) {
        var template = Handlebars.compile($('#template').html());
        $.each(results, function(i, context) {
            $('table.entries').append(template(context));
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题