我有许多文本输入元素,当其值发生更改时,将触发 fetchlistingListView的 collection listingCollection,然后通过如下所示listingListView的函数使用新数据进行更新。listingListView.refreshList我正在使用 PHP/Codeigniter 来公开 RESTful API。
问题:如果从fetch(). 但是,当过滤器没有返回结果时,服务器端和客户端应该如何处理呢?目前 Chrome 的 javascript 控制台显示404错误,并且在 Network 选项卡中,XHR 请求以红色突出显示。如果返回零结果,我要做的就是将其置空listingListView并显示一条消息,例如(没有返回结果),并且不在 javascript 控制台中引发任何错误。谢谢!
PHP 代码
function listings_get() {
    // Get filters
    $price_min = $this->get('price_min');
    $this->load->model('app_model');
    $results = $this->app_model->get_listings($price_min);
    if($results)
        $this->response($results);
    else 
        $this->response(NULL);
}
JS代码
window.ListingListView = Backbone.View.extend({
    tagName: 'table',
    initialize: function() {
        this.model.bind('reset', this.refreshList, this);
        this.model.bind('add', function(listing) {
            $(this.el).append(new ListingListItemView({ model: listing }).render().el);
        }, this);
    },
    render: function() {
        _.each(this.model.models, function(listing) {
            $(this.el).append(new ListingListItemView({ model: listing }).render().el);
        }, this);
        return this;
    },
    close: function() {
        $(this.el).unbind();
        $(this.el).empty();
    },
    refreshList: function() {
        $(this.el).empty();
        this.render();
    }
});