title: backbone.js: overwritting toJSON
body: <p>I am trying to implement some sort of nested collections in a Backbone.Model</p>
  
  <p>To do this I have to overwrite the adapter functions which parse the server response and wrap the array into a collection and the function which serializes the whole object without any helper methods. I am having problems with the second one.</p>
  
  <pre><code>var Model = Backbone.Model.extend({
  
      urlRoot: "/model",
  
      idAttribute: "_id",
  
      // this wraps the arrays in the server response into a backbone  collection
      parse: function(resp, xhr) {
          $.each(resp, function(key, value) {
              if (_.isArray(value)) {
                  resp[key] = new Backbone.Collection(value);
              } 
          });
          return resp;
      },
  
      // serializes the data without any helper methods
      toJSON: function() {
          // clone all attributes
          var attributes = _.clone(this.attributes);
  
          // go through each attribute
          $.each(attributes, function(key, value) {
              // check if we have some nested object with a toJSON method
              if (_.has(value, 'toJSON')) {
                  // execute toJSON and overwrite the value in attributes
                  attributes[key] = value.toJSON();
              } 
          });
  
          return attributes;
      }
  
  });
  </code></pre>
  
  <p>The problem is now at the second part in toJSON.
  For some reason</p>
  
  <pre><code>_.has(value, 'toJSON') !== true
  </code></pre>
  
  <p>doesn't return true</p>
  
  <p>Could somebody tell me what is going wrong?</p>