-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
adding feedback to update event of collection #3711
Changes from 7 commits
f55d3b5
c45b27f
57db882
1dacae8
434fcbb
cea9ed1
2929423
6298647
c5db447
2c3a1e8
41c04c5
077d19e
108ab3b
79dae25
86740b6
b13581d
6911ecc
7e862eb
c9c1ed6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1626,31 +1626,31 @@ | |
collection.add([{id: 3}], {at: '1'}); | ||
deepEqual(collection.pluck('id'), [1, 3, 2]); | ||
}); | ||
test("adding multiple models triggers `set` event once", 1, function() { | ||
test("adding multiple models triggers `update` event once", 1, function() { | ||
var collection = new Backbone.Collection; | ||
collection.on('update', function() { ok(true); }); | ||
collection.add([{id: 1}, {id: 2}, {id: 3}]); | ||
}); | ||
|
||
test("removing models triggers `set` event once", 1, function() { | ||
test("removing models triggers `update` event once", 1, function() { | ||
var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]); | ||
collection.on('update', function() { ok(true); }); | ||
collection.remove([{id: 1}, {id: 2}]); | ||
}); | ||
|
||
test("remove does not trigger `set` when nothing removed", 0, function() { | ||
test("remove does not trigger `update` when nothing removed", 0, function() { | ||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||
collection.on('update', function() { ok(false); }); | ||
collection.remove([{id: 3}]); | ||
}); | ||
|
||
test("set triggers `set` event once", 1, function() { | ||
test("set triggers `update` event once", 1, function() { | ||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||
collection.on('update', function() { ok(true); }); | ||
collection.set([{id: 1}, {id: 3}]); | ||
}); | ||
|
||
test("set does not trigger `set` event when nothing added nor removed", 0, function() { | ||
test("set does not trigger `update` event when nothing added nor removed", 0, function() { | ||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||
collection.on('update', function() { ok(false); }); | ||
collection.set([{id: 1}, {id: 2}]); | ||
|
@@ -1671,4 +1671,98 @@ | |
collection.invoke('method', 1, 2, 3); | ||
}); | ||
|
||
test("#3711 - remove's `update` event returns one removed model", function() { | ||
var model = new Backbone.Model({id: 1, title: 'First Post'}); | ||
var collection = new Backbone.Collection([model]); | ||
collection.on('update', function(context, options) { | ||
if (options.removedModels[0] !== model) { | ||
ok(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. notStrictEqual() |
||
} else { | ||
ok(true); | ||
} | ||
}); | ||
collection.remove(model); | ||
}); | ||
|
||
test("#3711 - remove's `update` event returns multiple removed models", function() { | ||
var model = new Backbone.Model({id: 1, title: 'First Post'}); | ||
var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); | ||
var collection = new Backbone.Collection([model, model2]); | ||
collection.on('update', function(context, options) { | ||
var removedModels = options.removedModels; | ||
if (!removedModels || removedModels.length !== 2) ok(false); | ||
if (removedModels.indexOf(model) > -1 && removedModels.indexOf(model2) > -1) { | ||
ok(true); | ||
} else { | ||
ok(false); | ||
} | ||
}); | ||
collection.remove([model, model2]); | ||
}); | ||
|
||
test("#3711 - set's `update` event returns one added model", function() { | ||
var model = new Backbone.Model({ id: 1, title: 'First Post'}); | ||
var collection = new Backbone.Collection(); | ||
collection.on('update', function(context, options) { | ||
var addedModels = options.addedModels; | ||
if (!addedModels || addedModels.length !== 1) ok(false); | ||
if (addedModels[0] === model) { | ||
ok(true); | ||
} else { | ||
ok(false); | ||
} | ||
}); | ||
collection.set(model); | ||
}); | ||
|
||
test("#3711 - set's `update` event returns multiple added models", function() { | ||
var model = new Backbone.Model({ id: 1, title: 'First Post'}); | ||
var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); | ||
var collection = new Backbone.Collection(); | ||
collection.on('update', function(context, options) { | ||
var addedModels = options.addedModels; | ||
if (!addedModels || addedModels.length !== 2) ok(false); | ||
if (addedModels[0] === model && addedModels[1] === model2) { | ||
ok(true); | ||
} else { | ||
ok(false); | ||
} | ||
}); | ||
collection.set([model, model2]); | ||
}); | ||
|
||
test("#3711 - set's `update` event returns one removed model", function() { | ||
var model = new Backbone.Model({ id: 1, title: 'First Post'}); | ||
var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); | ||
var model3 = new Backbone.Model({id: 3, title: 'My Last Post'}); | ||
var collection = new Backbone.Collection([model]); | ||
collection.on('update', function(context, options) { | ||
var removedModels = options.previousModels; | ||
if (!removedModels || removedModels.length !== 1) ok(false); | ||
if (removedModels[0] === model) { | ||
ok(true); | ||
} else { | ||
ok(false); | ||
} | ||
}); | ||
collection.set([model2, model3]); | ||
}); | ||
|
||
test("#3711 - set's `update` event returns multiple removed models", function() { | ||
var model = new Backbone.Model({ id: 1, title: 'First Post'}); | ||
var model2 = new Backbone.Model({id: 2, title: 'Second Post'}); | ||
var model3 = new Backbone.Model({id: 3, title: 'My Last Post'}); | ||
var collection = new Backbone.Collection([model, model2]); | ||
collection.on('update', function(context, options) { | ||
var removedModels = options.previousModels; | ||
if (!removedModels || removedModels.length !== 2) ok(false); | ||
if (removedModels[0] === model && removedModels[1] === model2) { | ||
ok(true); | ||
} else { | ||
ok(false); | ||
} | ||
}); | ||
collection.set([model3]); | ||
}); | ||
|
||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just always set
addedModels
andpreviousModels
on options and trigger updates only if theres alength
change. Seems mostly harmless (actually better IMO as the user won't have to check iftoAdd
is an array or not)If in agreement requires a simple test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i saw the same behavior in the set method, previousModels is only set when there are previousModels, so i just copied that, normally i would also be in favor of having it all the time, but i didnt want to change how the other methods do work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only other
previousModels
always attaches. I think attaching an empty array here is fine as well.Also, can we rename this
removedModels
?previousModels
makes sense in#reset
where every model is "previous", but here only a subset are being removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thank you for your feedback