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

adding feedback to update event of collection #3711

Closed
wants to merge 19 commits into from
Closed

adding feedback to update event of collection #3711

wants to merge 19 commits into from

Conversation

linus-amg
Copy link

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

Linus-A. M. Gubenis and others added 3 commits July 13, 2015 09:48
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;
Copy link
Collaborator

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?

Copy link
Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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?

Copy link
Author

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)

@megawac
Copy link
Collaborator

megawac commented Jul 13, 2015

👍

@linus-amg
Copy link
Author

i would also like to include mergedModels but i think that wont be possible (see #3708)

@jridgewell
Copy link
Collaborator

Mind adding the removed models to the update event in #remove?

@linus-amg
Copy link
Author

sure

elgubenis added 3 commits July 13, 2015 10:33
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>
@jridgewell
Copy link
Collaborator

Looks good to me, the only thing I want feedback on is the addedModels/removedModels naming.

@linus-amg
Copy link
Author

yeah, i didnt like it either, but since .reset returns previousModels, i thought they should be named alike, well lets wait for the feedback.

@linus-amg
Copy link
Author

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

@jridgewell
Copy link
Collaborator

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?

👍

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>
@jridgewell jridgewell mentioned this pull request Jul 20, 2015
var collection = new Backbone.Collection([model]);
collection.on('update', function(context, options) {
if (options.removedModels[0] !== model) {
ok(false);
Copy link
Collaborator

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.

Copy link
Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notStrictEqual()

elgubenis added 3 commits August 17, 2015 12:46
Signed-off-by: elgubenis <elgubenis@gmail.com>
* origin/develop:
  close feedback
Signed-off-by: elgubenis <elgubenis@gmail.com>
@linus-amg
Copy link
Author

sorry, fixed the test a little too late

@jridgewell
Copy link
Collaborator

LGTM.

elgubenis added 2 commits August 17, 2015 13:17
Signed-off-by: elgubenis <elgubenis@gmail.com>
Signed-off-by: elgubenis <elgubenis@gmail.com>
@jashkenas
Copy link
Owner

Naming isn't quite there yet — but more importantly without some sort of mergedModels, this change feels incomplete. Having the full bookkeeping of changes to models isn't possible?

@jridgewell
Copy link
Collaborator

We can add a toMerge here, and push to it here.

@jashkenas
Copy link
Owner

Alright. And there isn't a big perf cost from the extra bookkeeping?

Naming-wise added, removed, merged sounds good. Perhaps even under a sub-object if necessary.

@jridgewell
Copy link
Collaborator

Alright. And there isn't a big perf cost from the extra bookkeeping?

With everything else this method is doing, I don't image one.

@linus-amg
Copy link
Author

hi, im working on the PR, how could the sub-object be named? changes, status, updates, move(s)/ments, details, models?

@linus-amg
Copy link
Author

i will dub it 'changes' for now if thats ok

elgubenis added 2 commits August 19, 2015 08:40
Signed-off-by: elgubenis <elgubenis@gmail.com>
Signed-off-by: elgubenis <elgubenis@gmail.com>
@jridgewell
Copy link
Collaborator

@elgubenis: You'll have to rebase off of the current master for this to merge cleanly.

elgubenis added 3 commits August 19, 2015 10:04
# 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>
@linus-amg
Copy link
Author

will write some tests for the merging

elgubenis added 2 commits August 19, 2015 10:43
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>
@jridgewell
Copy link
Collaborator

LGTM.

@jridgewell jridgewell mentioned this pull request Oct 27, 2015
10 tasks
@jridgewell
Copy link
Collaborator

@elgubenis: Mind rebasing? I'll merge this afterwards.

@mordentware
Copy link

Just wanted to add that I was after this exact functionality, cheers guys for implementing. Any chance of getting the conflicts sorted out?

@linus-amg
Copy link
Author

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 = {};
Copy link
Collaborator

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
}

@megawac
Copy link
Collaborator

megawac commented Jan 27, 2016

@elgubenis mind addressing @akre54 feedback and we'll merge? Thanks

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

Successfully merging this pull request may close these issues.

6 participants