jashkenas/backbone — pull request #2959 by wyuenho (2014-01-15 19:13:49+00:00)
Title: Native view

### This PR improves on PR #2865:

- Added a very small polyfill of `addEventListener` and `removeEventListener` for IE8.
- Slightly faster and a much more compact `matchesSelector` implementation
- Fixed a bug with `remove` where PR#2865 confuses removal with detachment. `$.fn.remove` will remove event handlers while PR#2865 does not. Failure to do so will make some event handlers that depends on this behavior fail. This broke Backgrid.
- A native `setElement` that's more faithful to jQuery's behavior. The code is improved from [Backbone.native](https://github.com/inkling/backbone.native/blob/master/backbone.native.js#L225). Specifically, you can supply an HTML snippet, a CSS selector or a DOM element, just like jQuery.
- No more `useNative` flag and branching. The old View had been extracted into BaseView and `_ensureElement`, `setElement`, `delegateEvents` and `undelegateEvents` uses native APIs. So Backbone's will have a baseline View that's fast by default.
- `BaseView#$` just delegates to `this.el.querySelectorAll` and returns a node list.
- Removes `View#find` and `View#findAll`. They add no value and it's not even the correct implementation of proposed [Selectors API Level 2](http://www.w3.org/TR/2012/WD-selectors-api2-20120628/#the-apis) spec.
- Introduces `_delegateEvents` and `_undelegateEvents` for subclasses to override.
- Much much more compact implementation of `BaseView#_delegateEvent` and `BaseView#_undelegateEvents` in place of `util.delegate` and `util.undelegate`. `BaseView#_delegateEvent` will also work on IE8.
- Removes the superfluous jQuery shim that's `utils`. This PR has effectively removes hard dependency on jQuery already by pulling out a BaseView that's basically the old view + 2 hooks and all native DOM API calls. The only remaining dep on `$.ajax` can be overcome easily by replacing Backbone.ajax with one of the many $.ajax alternative implementations out there.
- The new `Backbone.View` is a 26 line extremely light weight subclass of `Backbone.BaseView` that's 100% backward compatible with the old jQuery-based View.
- Comments everywhere and better tests.

### Tests passed on:
IE 8+
Chrome
Firefox
Safari
Opera

### Performance:
http://jsperf.com/backbone-patch-22be8f9/2

Around 70% faster than the jQuery-based View and slightly faster than #2865, by a couple of percentages consistently.

### Remaining issues:
- `npm test` currently bombs out because the code injection trip from npm to PhantomJS somehow triggered the CommonJS UMD block and `Element` is no where to be found from the global scope. I'm not sure how to fix this. Putting the polyfills back to an object will slow the BaseView back down because of the extra property accesses. IMHO BaseView should be as fast as possible and as compatible as possible. Someone help me with this?
- Should `BaseView` be called something else? Should the native View subclass a refactored jQuery-based View instead? Code size will be exactly the same.