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

Follow up for Chaplin#780 #2

Merged
merged 3 commits into from
Jan 14, 2015
Merged

Follow up for Chaplin#780 #2

merged 3 commits into from
Jan 14, 2015

Conversation

Florian-R
Copy link

Not green yet. Still one test failing.

@@ -30,7 +30,11 @@ insertView = (list, viewEl, position, length, itemSelector) ->

if insertInMiddle or itemSelector
# Get the children which originate from item views.
children = list.querySelectorAll(itemSelector)
if itemSelector
Copy link
Owner

Choose a reason for hiding this comment

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

I think the Chaplin style is

children = if itemSelector
    list.querySelectorAll(itemSelector)
  else
    list.children

@Florian-R
Copy link
Author

Fixed the coding style but i'm starting to think it wasn't worth push this. I've seen @paulmillr suggestion to throw early just after, but after some investigations, i 'm afraid it might be a breaking change. This jsbin demonstrate visually the behavior on master of a CollectionView with an itemSelector with or without listSelector.

I've double checked with the tests on master with exos setup and this test pass, so it's definitively something supported.

The failure come from qsa which retrieve all the matching nodes, as opposed to the implementation on master which rely on Backbone.utils.matchesSelector (exposed by Exoskeleton) and filter only the directs children.

Maybe we can come up with something like, based on my previous attempt and code from Exos (quickly done, untested and ugly)

matchesSelector: (node, selector) ->
  matches = node.matches || node.webkitMatchesSelector || node.mozMatchesSelector || node.msMatchesSelector

  if !matchesSelector
    throw new Error('Element#matches is not supported');

  matches.call(node, selector)

filterChildren = (nodeList, selector) ->
  return nodeList unless selector
  for node in nodeList when matchesSelector node, selector
    if $ and $(node).is(selector)
      node
    else if matchesSelector.call(node, selector)
      node

@akre54
Copy link
Owner

akre54 commented Jan 13, 2015

Why not prepend the > selector to the qsa to select only child nodes?

list.querySelectorAll("> #{itemSelector}")

@Florian-R
Copy link
Author

It could have been a nice solution indeed... But it throws an error in every browsers.

A quick googling seems to indicate there's only very contrived solutions to use querySelectorAll with a child selector.

@akre54
Copy link
Owner

akre54 commented Jan 13, 2015

Ah crap. I remember that. (It's a specific reason why we didn't move off jQuery a few years back, for instance).

Any reason not to use this shim from Backbone.NativeView?

@akre54
Copy link
Owner

akre54 commented Jan 13, 2015

The matches on children vs listSelector isn't ideal, but I can't imagine it would break many if any things.

I'd also be fine dropping IE8 support, since almost every other browser supports matches natively.

@Florian-R
Copy link
Author

I'd also be fine dropping IE8 support, since almost every other browser supports matches natively.

I've mixed feeling about that. It will require a major version bump, no?

I'd rather go with the same shim than Backbone.NativeView, minus the prefixed version (excepted msMatchesSelector maybe).

If we go down this route, the remaining question is do we expose matchesSelector in Chaplin.utils (can be useful in a no jQuery world, but i'm unsure it belongs here) or do we keep it private?

@akre54
Copy link
Owner

akre54 commented Jan 13, 2015

I've mixed feeling about that. It will require a major version bump, no?

I'd imagine any changes we make here would require a major version bump.

I'd rather go with the same shim than Backbone.NativeView, minus the prefixed version (excepted msMatchesSelector maybe).

I dunno.... Firefox just dropped the moz prefix 2 versions ago, and webkit (safari and chrome both) weren't that much longer than that. It doesn't hurt anything, and we can always drop them down the line.

I'm fine exposing on Chaplin.utils. Would be easier to override if someone has a better shim (or our implementation doesn't work with their selector on older browsers).

@akre54
Copy link
Owner

akre54 commented Jan 13, 2015

Feel free to make that change and I'll merge

@Florian-R
Copy link
Author

Change done, might need small tweaks to fit the coding style

Feel free to make that change and I'll merge

My first idea was to ask you to not merge as is, as i intend to fix the tests with the Exos setup.
But as you've opened #3, maybe it's best to merge this, implement the suggested changes an then go back to fix the tests?

Sidenote. If this get merged, maybe it's a good idea to squash 1c63e3e with the last commit to keep the original PR clean.


(node, selector) ->
matches.call(node, selector)

Copy link
Author

Choose a reason for hiding this comment

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

Implementation is strictly the same than NativeView's one, except i've just slighly tweaked it to match the construct of beget, indexOf etc. Let me know if you prefer to keep it like the original.

Copy link
Owner

Choose a reason for hiding this comment

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

In general I'm not a fan of functions wrapping a call just to preserve a signature. I think it's fine if in filterChildren we do utils.matchesSelector.call node, selector instead

@Florian-R
Copy link
Author

Should be OK now. Commited separately 969a9e3 so it can be easily reverted / squashed in the original PR.

akre54 added a commit that referenced this pull request Jan 14, 2015
@akre54 akre54 merged commit 048a406 into akre54:bb-view-hooks Jan 14, 2015
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