AutofocusableTags = ['input', 'textarea', 'button'];

var autofocusSelector = _.map(AutofocusableTags, function (tag) {
  return tag + '[autofocus]';
}).join(', ');

var _super = Template.prototype.constructView;

Template.prototype.constructView = function (/* arguments */) {
  var args = Array.prototype.slice.call(arguments);
  var view = _super.apply(this, args);
  view.onViewReady(function () {
    var tpl = Template.instance();
    var autofocusField = tpl && tpl.find(autofocusSelector);
    if (autofocusField) {
      autofocusField.focus();
    }
  });
  return view;
};

AutofocusableTags = ['input', 'textarea', 'button'];

var autofocusSelector = _.map(AutofocusableTags, function (tag) {
  return tag + '[autofocus]';
}).join(', ');

var autofocusRenderedCallback = function() {
  var autofocusField = this.find(autofocusSelector);
  if (autofocusField) {
    autofocusField.focus();
  }
}

var _super = Template.prototype._getCallbacks;
Template.prototype._getCallbacks = function(which) {
  var callbacks = _super.call(this, which);
  if (which === 'rendered') {
    callbacks.push(autofocusRenderedCallback);
  }
  return callbacks;
};

Fix the implementation for the upcoming release of Meteor (v1.0.4)
Fix the implementation for the upcoming release of Meteor (v1.0.4)

The new release supports multiple `onRendered` callbacks per instance.
This new meteor feature broke our implementation, so considering that
we had to change it we now take advantage of the multiple callbacks
per template to simplify our implementation.

Thus, we now "simply" register an autofocus callback for all
templates.
