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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@
// The JSON representation of a Collection is an array of the
// models' attributes.
toJSON: function() {
return this.map(function(model){ return model.toJSON(); });
var self_arguments = arguments;
return this.map(function(model){ return model.toJSON.apply(model,self_arguments); });
},

// Add a model, or list of models to the set. Pass **silent** to avoid
Expand Down
18 changes: 14 additions & 4 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ $(document).ready(function() {
var sync = Backbone.sync;

var a, b, c, d, e, col, otherCol;

var TestModel = Backbone.Model.extend({
toJSON: function(opts) {
var obj = Backbone.Model.prototype.toJSON.call(this),
noId = (opts && opts.noId === true);
if (noId) delete obj.id;
return obj;
}
});

module("Backbone.Collection", {

setup: function() {
a = new Backbone.Model({id: 3, label: 'a'});
b = new Backbone.Model({id: 2, label: 'b'});
c = new Backbone.Model({id: 1, label: 'c'});
d = new Backbone.Model({id: 0, label: 'd'});
a = new TestModel({id: 3, label: 'a'});
b = new TestModel({id: 2, label: 'b'});
c = new TestModel({id: 1, label: 'c'});
d = new TestModel({id: 0, label: 'd'});
e = null;
col = new Backbone.Collection([a,b,c,d]);
otherCol = new Backbone.Collection();
Expand Down Expand Up @@ -383,6 +392,7 @@ $(document).ready(function() {

test("Collection: toJSON", function() {
equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
equal(JSON.stringify(col.toJSON({noId:true})), '[{"label":"a"},{"label":"b"},{"label":"c"},{"label":"d"}]');
});

test("Collection: Underscore methods", function() {
Expand Down