Permalink
Browse files

Allow alternate clicks for href handling - Fixes emberjs#1096

  • Loading branch information...
wagenet committed Jul 12, 2012
1 parent 2a31b5f commit e668276a3aab382e145c3bc7afd059a9a6438534
@@ -108,3 +108,56 @@ test("it sets an URL with a context", function() {
ok(view.$().html().match(/href=['"].*\/dashboard\/1['"]/), "The html (" + view.$().html() + ") has the href /dashboard/1 in it");
});
test("it does not trigger action with special clicks", function() {
var dispatcher = Ember.EventDispatcher.create();
dispatcher.setup();
var showCalled = false;
var view = Ember.View.create({
template: compile("<a {{action show href=true}}>Hi</a>")
});
var controller = Ember.Object.create(Ember.ControllerMixin, {
target: {
urlForEvent: function(event, context) {
return "/foo/bar";
},
show: function() {
showCalled = true;
}
}
});
Ember.run(function() {
view.set('controller', controller);
view.appendTo('#qunit-fixture');
});
function checkClick(prop, value, expected) {
var event = Ember.$.Event("click");
event[prop] = value;
view.$('a').trigger(event);
if (expected) {
ok(showCalled, "should call action with "+prop+":"+value);
ok(event.isDefaultPrevented(), "should prevent default");
} else {
ok(!showCalled, "should not call action with "+prop+":"+value);
ok(!event.isDefaultPrevented(), "should not prevent default");
}
}
checkClick('ctrlKey', true, false);
checkClick('altKey', true, false);
checkClick('metaKey', true, false);
checkClick('shiftKey', true, false);
checkClick('button', 1, false);
checkClick('button', 0, true);
Ember.run(function() {
dispatcher.destroy();
});
});
@@ -6,12 +6,19 @@ var ActionHelper = EmberHandlebars.ActionHelper = {
registeredActions: {}
};
ActionHelper.registerAction = function(actionName, eventName, target, view, context) {
ActionHelper.registerAction = function(actionName, eventName, target, view, context, link) {
var actionId = (++Ember.$.uuid).toString();
ActionHelper.registeredActions[actionId] = {
eventName: eventName,
handler: function(event) {
if (link && (event.button !== 0 || event.shiftKey || event.metaKey || event.altKey || event.ctrlKey)) {
// Allow the browser to handle special link clicks normally
return;
}
event.preventDefault();
event.view = view;
event.context = context;
@@ -176,7 +183,7 @@ EmberHandlebars.registerHelper('action', function(actionName, options) {
var hash = options.hash,
eventName = hash.on || "click",
view = options.data.view,
target, context, controller;
target, context, controller, link;
view = get(view, 'concreteView');
@@ -195,9 +202,10 @@ EmberHandlebars.registerHelper('action', function(actionName, options) {
if (hash.href && target.urlForEvent) {
url = target.urlForEvent(actionName, context);
output.push('href="' + url + '"');
link = true;
}
var actionId = ActionHelper.registerAction(actionName, eventName, target, view, context);
var actionId = ActionHelper.registerAction(actionName, eventName, target, view, context, link);
output.push('data-ember-action="' + actionId + '"');
return new EmberHandlebars.SafeString(output.join(" "));
@@ -139,7 +139,6 @@ Ember.EventDispatcher = Ember.Object.extend(
handler = action.handler;
if (action.eventName === eventName) {
evt.preventDefault();
return handler(evt);
}
});

0 comments on commit e668276

Please sign in to comment.