Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用backbone.js从服务器渲染数据_Php_Javascript_Backbone.js - Fatal编程技术网

Php 如何使用backbone.js从服务器渲染数据

Php 如何使用backbone.js从服务器渲染数据,php,javascript,backbone.js,Php,Javascript,Backbone.js,首先要了解的是,这是我从服务器得到的响应 $response = array(); $i = 0; while($result = mysql_fetch_assoc($query)){ $response[$i]["id"] = $result["ID"]; $response[$i]["post_date"] = $result["post_dat

首先要了解的是,这是我从服务器得到的响应

$response = array();
            $i = 0;
            while($result = mysql_fetch_assoc($query)){
                $response[$i]["id"]             = $result["ID"];
                $response[$i]["post_date"]      = $result["post_date"];
                $response[$i]["post_content"]   = $result["post_content"];
                $response[$i]["nickname"]       = $result["nickname"];
                $i++;
            }
            echo json_encode($response);
看起来@k33g_org的方式确实有效,但现在控制台告诉我未捕获的引用错误:未定义昵称

我的模板是

<script type="text/template" id="tplhome_post_list">
            <div class="post">
                <div class="view">
                    <p class="header_post">
                        <%= nickname %> - Le <%= post_date %>
                    </p>
                    <div class="statut">
                        <div class="like">
                            8
                        </div>
                        <div class="reply">
                            25
                        </div>                          
                    </div>
                    <p class="clear">
                        <%= post_content %>
                    </p>
                </div>
            </div>
        </script>

怎样才能修复它??定义模板值???

您必须像这样编写获取:

Posts.fetch({
    success:function(data) {
       /* when you get data from server request data variable is populated */
       /* then you can call render() method of the view */
    }, 
    error : function(err) { 
      throw "Houston we've got a problem...";
    }
});
您可以像这样将数据传递给render方法

    render : function(data) {
        var renderedContent = this.template(this.data.toJSON());
        $(this.el).html(renderedContent);
        return this;
    }
或者,更好的方法是,您可以将集合的重置事件绑定到视图,在视图的初始化方法中,放置以下内容:

_.bindAll(this, 'render');
this.collection.bind('change', this.render);
this.collection.bind('add', this.render);
this.collection.bind('remove', this.render);

然后,当集合内容发生更改时,即在抓取时,将调用我在编辑时编写的“渲染”命令。