-
-
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
Conversation
collection's update event receives 2 additional attributes, in case of removal of existing models, previosModels is an array containing the removed models which where references to the collection before .set was called. The second attribute, addedModels contains an array of models which were added to the collection calling the .set method. Signed-off-by: elgubenis <elgubenis@gmail.com>
# By Linus-A. M. Gubenis # Via Linus-A. M. Gubenis * 'master' of https://github.com/elgubenis/backbone: Update collection.js
if (!options.silent) { | ||
var addOpts = at != null ? _.clone(options) : options; | ||
for (var i = 0; i < toAdd.length; i++) { | ||
if (at != null) addOpts.index = at + i; | ||
(model = toAdd[i]).trigger('add', model, this, addOpts); | ||
} | ||
if (toAdd.length || previousModels.length) { | ||
if (toAdd.length) options.addedModels = toAdd; | ||
if (previousModels.length) options.previousModels = previousModels; |
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.
is previousModels
the right term or should it be removed models?
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 thought it should be removedModels, but i saw that .reset returns previousModels aswell, so i think keeping both the same would not be wrong.
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.
Mind calling these options.add
and options.remove
? It mirrors the add
and remove
event names.
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 think past tense works better here, added
& 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.
I'm pro past tense, but nothing else in Backbone's public API uses it. @jashkenas, @braddunbar, or @akre54 want to weigh in?
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.
options.add and options.remove are already set, because they are the options passed to the .set (like options.merge)
👍 |
i would also like to include mergedModels but i think that wont be possible (see #3708) |
Mind adding the removed models to the |
sure |
returning removedModels, previousModels and addedModels Signed-off-by: elgubenis <elgubenis@gmail.com>
* develop: update events of remove/set
Signed-off-by: elgubenis <elgubenis@gmail.com>
Looks good to me, the only thing I want feedback on is the |
yeah, i didnt like it either, but since .reset returns previousModels, i thought they should be named alike, well lets wait for the feedback. |
should i write some tests to see that the events in .remove return the removed models which it should return? as well as the removed/previous and added ones for .set? or is that not necessary |
👍 |
these 6 tests check if the update event for the remove and set methods return added and removed models. - remove 1 model returns the 1 model removed - remove multiple models, returns the multiple models removed - setting 1 model, returns the 1 model added - setting multiple models, return the multiple models added - setting 2 models when there was 1 model at first, returns the 1 model removed - setting 1 model when there were 2 models at first, returns the 2 models removed. Signed-off-by: elgubenis <elgubenis@gmail.com>
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Try just ok(options.removedModels[0] === model)
or even better, strictEqual(options.removedModels[0], model)
instead of this if
block.
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.
like that? !strictEqual(options.removedModels[0], model)
i guess?
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.
notStrictEqual()
Signed-off-by: elgubenis <elgubenis@gmail.com>
* origin/develop: close feedback
Signed-off-by: elgubenis <elgubenis@gmail.com>
sorry, fixed the test a little too late |
LGTM. |
Signed-off-by: elgubenis <elgubenis@gmail.com>
Signed-off-by: elgubenis <elgubenis@gmail.com>
Naming isn't quite there yet — but more importantly without some sort of |
Alright. And there isn't a big perf cost from the extra bookkeeping? Naming-wise |
With everything else this method is doing, I don't image one. |
hi, im working on the PR, how could the sub-object be named? changes, status, updates, move(s)/ments, details, models? |
i will dub it 'changes' for now if thats ok |
Signed-off-by: elgubenis <elgubenis@gmail.com>
Signed-off-by: elgubenis <elgubenis@gmail.com>
@elgubenis: You'll have to rebase off of the current master for this to merge cleanly. |
# By Justin Ridgewell (18) and others # Via Graeme Yeates (6) and others * 'master' of https://github.com/jashkenas/backbone: (35 commits) kill old email fix localStorage link include -> contains doc fixes and tightening Add Modernizr style check for 'onhashchange' to avoid issues in IE compat modes. [closes #3730] Draft change log for Backbone 1.3 Test null set doesn't clear model. Use some, includes, map in tests instead of any, include, pluck. Setup DOM for every view test Use `_.bind` instead of `context` param of `_.sortBy`. Add Collection#includes for underscore v1.8 Update index.html: adjust toc_section font fix grammario update docs according to issue #3694 Fix #3724; add qunit dependency Refactor example code in View#events docs style cleanup Add docs for View#events typos Update collection.js ... Signed-off-by: elgubenis <elgubenis@gmail.com> # Conflicts: # backbone.js
added, removed and merged models Signed-off-by: elgubenis <elgubenis@gmail.com>
Signed-off-by: elgubenis <elgubenis@gmail.com>
will write some tests for the merging |
Signed-off-by: elgubenis <elgubenis@gmail.com>
update gets not fired anymore because models are not added anymore, and it does not get merged either. Signed-off-by: elgubenis <elgubenis@gmail.com>
LGTM. |
@elgubenis: Mind rebasing? I'll merge this afterwards. |
Just wanted to add that I was after this exact functionality, cheers guys for implementing. Any chance of getting the conflicts sorted out? |
hi, will do. |
@@ -912,7 +917,13 @@ | |||
model.trigger('add', model, this, options); | |||
} | |||
if (sort || orderChanged) this.trigger('sort', this, options); | |||
if (toAdd.length || toRemove.length) this.trigger('update', this, options); | |||
if (toAdd.length || toRemove.length || toMerge.length) { | |||
options.changes = {}; |
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.
minor style nit:
options.changes = {
added: toAdd,
removed: toRemove,
merged: toMerge
}
@elgubenis mind addressing @akre54 feedback and we'll merge? Thanks |
collection's update event receives 2 additional attributes, in case of removal of existing models, previousModels is an array containing the removed models which where referenced to the collection before .set was called. The second attribute, addedModels contains an array of models which were added to the collection calling the .set method.
Fixes #3700