Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy collection toJSON arguments to model #1098

Closed
wants to merge 1 commit into from
Closed

proxy collection toJSON arguments to model #1098

wants to merge 1 commit into from

Conversation

troyk
Copy link

@troyk troyk commented Mar 14, 2012

I often pass arguments to a overridden toJSON method on the model for different views etc, but there is no support for this at the collection level, e.g.:

// to include a nested collection in the json
myCollection.toJSON({includeOwners:true});

The attached code changes the collection's toJSON to pass it's arguments like so:

// The JSON representation of a Collection is an array of the
// models' attributes.
toJSON: function() {
  var self_arguments = arguments;
  return this.map(function(model){ return model.toJSON.apply(model,self_arguments); });
}

tests included. --thanks, troy

@jashkenas
Copy link
Owner

Good idea -- but I don't think we need to formalize the API quite like that. Let's just pass along an options object.

@jashkenas jashkenas closed this in 9414513 Mar 14, 2012
@troyk
Copy link
Author

troyk commented Mar 14, 2012

@jashkenas Thanks for getting this in so quick, I went the apply route to keep the api informal allowing both toJSON(arg1, arg2, ..) and toJSON({ .. })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants