title: Overriding backbone's parse function
body: <p>I'm trying to use Backbone with an API.</p>
  
  <p>The default API response format is:</p>
  
  <pre><code>{
  somemetadatas:xxx , 
  results:yyy
  }
  </code></pre>
  
  <p>Whether it's a fetch for a single model or a collection.</p>
  
  <p>So as far as I know I can override the Backbone <code>parse</code> function with:</p>
  
  <pre><code>parse: function (response) {
      return response.results;
  },
  </code></pre>
  
  <p>But I've seen in <a href="http://backbonejs.org/#Collection-parse" rel="noreferrer">the documentation</a>:</p>
  
  <blockquote>
    <p><strong>parse</strong> <code>collection.parse(response)</code></p>
    
    <p><strong>parse</strong> is called by Backbone whenever
    a collection's models are returned by the server, in fetch. The
    function is passed the raw <code>response</code> object, and should return the
    array of model attributes to be added to the collection. The default
    implementation is a no-op, simply passing through the JSON response.
    Override this if you need to work with a preexisting API, or better
    namespace your responses. Note that afterwards, if your model class
    already has a <code>parse</code> function, it will be run against each fetched
    model.</p>
  </blockquote>
  
  <p>So if I have a response for a collection fetch like that:</p>
  
  <pre><code>{
  somemetadatas:xxx , 
  results:[user1,user2]
  }
  </code></pre>
  
  <p>The first <code>parse</code> function on the collection will extract <code>[user1,user2]</code>.</p>
  
  <p>But the doc says:</p>
  
  <blockquote>
    <p>Note that afterwards, if your model class
    already has a <code>parse</code> function, it will be run against each fetched
    model.</p>
  </blockquote>
  
  <p>So it will try to find <code>response.results;</code> on <code>user1</code> and <code>user2</code></p>
  
  <p>I need both parse functions on the model and collection because both model and collection datas will be under the result attribute.</p>
  
  <p>But if i fetch on a collection, I don't want the model parse function to be used againt a single array element.</p>
  
  <hr>
  
  <p>So is there a solution to this problem?</p>
  
  <p>I think of a solution where my collection parse function will transform something like this:</p>
  
  <pre><code>{
  somemetadatas:xxx , 
  results:[user1,user2]
  }
  </code></pre>
  
  <p>into something like this:</p>
  
  <pre><code>[ {results.user1} , {results.user2} ]
  </code></pre>
  
  <p>So that the model parse function will not fail on a collection fetch.
  But it's a bit hacky... is there any elegant solution to this problem?</p>
  
  <hr>
  
  <p>By the way, as my API will always produce results of this form, is it possible to override by default the <code>parse</code> function of all my models and collections? (Sorry i'm a JS noob...)</p>