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

[fix] ensure that the prevent default and stop prop occurs in the correct context #759

Merged
merged 1 commit into from
Oct 30, 2013
Merged
Show file tree
Hide file tree
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
[fix] ensure that the prevent default and stop prop occurs in the cor…
…rect context

reported here #758 (comment)
introduced in a790fb3
  • Loading branch information
samccone committed Oct 28, 2013
commit e63fe847793dfd2b922b7bdd175840adecb5dea9
40 changes: 40 additions & 0 deletions spec/javascripts/view.triggers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ describe("view triggers", function(){
});
});

describe("triggers should stop propigation and events by default", function() {
var myView = Backbone.Marionette.ItemView.extend({
triggers: {
'click h2': "headline:clicked"
},

initialize: function() {
this.spanClicked = false;
},

onRender: function() {
var self = this;
this.$('span').on("click", function() {
self.spanClicked = true;
});
},

template: _.template("<h2><span>hi</span></h2><a href='#hash-url'>hash link</a>")
});

var viewInstance;

beforeEach(function(){
viewInstance = new myView;
spyOn(window, 'onhashchange');

viewInstance.render();
viewInstance.$('h2').click();
viewInstance.$('a').click();
});

it("should stop propigation by default", function(){
expect(viewInstance.clicked).toBe(false);
});

it("should prevent default by default", function() {
expect(window.onhashchange).not.toHaveBeenCalled();
});
});

describe("when triggers items are manually configured", function(){
var View = Backbone.Marionette.ItemView.extend({
triggers: {
Expand Down
4 changes: 2 additions & 2 deletions src/marionette.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Marionette.View = Backbone.View.extend({
var shouldPrevent = hasOptions ? value.preventDefault : prevent;
var shouldStop = hasOptions ? value.stopPropagation : stop;

if (shouldPrevent && prevent) { prevent(); }
if (shouldStop && stop) { stop(); }
if (shouldPrevent && prevent) { prevent.apply(e); }
if (shouldStop && stop) { stop.apply(e); }
}

// build the args for the event
Expand Down