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

Slight optimization for Events, cache splitting Regex #1097

Merged
merged 1 commit into from
Mar 14, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
// Backbone.Events
// -----------------

// Regular expression used to split event strings
var eventSplitter = /\s+/;

// A module that can be mixed in to *any object* in order to provide it with
// custom events. You may bind with `on` or remove with `off` callback functions
// to an event; trigger`-ing an event fires all callbacks in succession.
Expand All @@ -87,7 +90,7 @@
on: function(events, callback, context) {
var calls, event, node, tail, list;
if (!callback) return this;
events = events.split(/\s+/);
events = events.split(eventSplitter);
calls = this._callbacks || (this._callbacks = {});
while (event = events.shift()) {
// Create an immutable callback list, allowing traversal during
Expand All @@ -111,7 +114,7 @@
if (!events) {
delete this._callbacks;
} else if (calls = this._callbacks) {
events = events.split(/\s+/);
events = events.split(eventSplitter);
while (event = events.shift()) {
node = calls[event];
delete calls[event];
Expand All @@ -137,7 +140,7 @@
var event, node, calls, tail, args, all, rest;
if (!(calls = this._callbacks)) return this;
all = calls.all;
events = events.split(/\s+/);
events = events.split(eventSplitter);
rest = slice.call(arguments, 1);
while (event = events.shift()) {
if (node = calls[event]) {
Expand Down Expand Up @@ -1088,7 +1091,7 @@
};

// Cached regex to split keys for `delegate`.
var eventSplitter = /^(\S+)\s*(.*)$/;
var delegateEventSplitter = /^(\S+)\s*(.*)$/;

// List of view options to be merged as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'];
Expand Down Expand Up @@ -1167,7 +1170,7 @@
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) throw new Error('Method "' + events[key] + '" does not exist');
var match = key.match(eventSplitter);
var match = key.match(delegateEventSplitter);
var eventName = match[1], selector = match[2];
method = _.bind(method, this);
eventName += '.delegateEvents' + this.cid;
Expand Down