1

首先获取信息,这是我来自服务器的响应

$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 方式确实有效,但现在控制台说我Uncaught ReferenceError: nickname is not defined

我的模板是

<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>

怎么解决?定义模板值??

4

1 回答 1

0

你必须这样写 fetch :

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 : function(data) {
        var renderedContent = this.template(this.data.toJSON());
        $(this.el).html(renderedContent);
        return this;
    }

或者,更好的是,您可以将集合的重置事件绑定到视图,在视图的initialize方法中,输入:

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

然后当集合内容改变时(即获取时),render()被调用

于 2012-11-09T10:10:59.087 回答