Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should backbone.js handle a GET request that returns no results?
    primarykey
    data
    text
    <p>I have a number of text input elements that when its values are changed, will trigger a fetch on <code>listingListView</code>'s collection <code>listingCollection</code>, which then updates <code>listingListView</code> with the new data via the function <code>listingListView.refreshList</code> as shown below. I am using PHP/Codeigniter to expose a RESTful API.</p> <p><strong>Problem:</strong> Everything works fine if there are results retrieved from the <code>fetch()</code>. However, when the filters results in no result being returned, how should the server side and the client side handle it? Currently Chrome's javascript console displays a <code>404</code> error and in the Network tab, the XHR request is highlighted in red. All I want to do in the event of zero results returned, is to blank the <code>listingListView</code> and show a message like <em>(No results returned)</em> and not throw any errors in the javascript console. Thanks!</p> <p><strong>PHP Code</strong></p> <pre><code>function listings_get() { // Get filters $price_min = $this-&gt;get('price_min'); $this-&gt;load-&gt;model('app_model'); $results = $this-&gt;app_model-&gt;get_listings($price_min); if($results) $this-&gt;response($results); else $this-&gt;response(NULL); } </code></pre> <p><strong>JS Code</strong></p> <pre><code>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(); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload