• Jump To … +
    chain.js classList.js collection.js delegate.js extend.js has.js invoke.js isBlank.js isFunction.js isObject.js isRegExp.js isString.js isUndefined.js mapEvents.js mapSelections.js matches.js pluck.js query.js queryAll.js result.js tap.js template.js tokenize.js util.js washi.js
  • mapEvents.js

  • ¶

    Given an element, an object of events, a pool of selectors, and a scope, this function binds those events within the provided scope.

    For example:

    mapEvents(window, { resize: method }, {}, obj)
    mapEvents(document.boyd, { 'click {items}': method }, { items: '.class' }, obj);
    
    var delegate   = require('./delegate');
    var tokenize   = require('./tokenize');
    var template   = require('./template');
    var isFunction = require('./isFunction');
    
    function bindEvent (el, name, method, selectors, scope) {
      var items  = name.split(',');
      var toCall = (isFunction(method) ? method : scope[method]).bind(scope);
    
      while (items.length) {
        var tokens = tokenize(template(items.pop(), selectors));
    
        delegate(el, tokens[0], tokens[1], toCall);
      }
    }
    
    module.exports = function(el, events, selectors, scope) {
      for (var name in events) {
        bindEvent(el, name, events[name], selectors, scope);
      }
    };