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

Fixing getManagerForEl when root element is provided #232

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions Backbone.CollectionBinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@
},

isElContained: function(findEl){
return this._el === findEl || $(this._el).has(findEl).length > 0;
if(this._el === findEl){
return true;
}

var $el = $(this._el);
return $el.has(findEl).length > 0 || $el.is(findEl);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but aren't these 2 statements the same thing?
this._el === findEl
$el.is(findEl)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that's my concern on root node : they "represent" the same DOM node, but are different instances

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could make a single $(this._el)[0] === $(findEl)[0] but this sound a bit hacky

However, I agree we could imagine making a single $el.is(findEl) test (removing this._el === findEl test)

},

getModel: function(){
Expand Down Expand Up @@ -295,7 +300,7 @@
},

isElContained: function(findEl){
return this._view.el === findEl || this._view.$el.has(findEl).length > 0;
return this._view.el === findEl || this._view.$el.has(findEl).length > 0 || this._view.$el.is(findEl);
},

getModel: function(){
Expand Down
1 change: 1 addition & 0 deletions spec/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<script type="text/javascript" src="javascripts/oneElementToMultipleAttributes.spec.js"></script>

<script type="text/javascript" src="javascripts/CollectionBinder/sorting.spec.js"></script>
<script type="text/javascript" src="javascripts/CollectionBinder/getManagerForEl.spec.js"></script>

</head>
<body>
Expand Down
60 changes: 60 additions & 0 deletions spec/javascripts/CollectionBinder/getManagerForEl.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe("CollectionBinder: sorting", function(){

function createViewWithClickCallback(clickCallback) {
var View = Backbone.View.extend({
events: function(){
return {
'click .clickable-div': this.onElementClicked,
'click .clickable-span': this.onElementClicked
};
},

initialize: function () {
var elManagerFactory = new Backbone.CollectionBinder.ElManagerFactory("<div class='clickable-div'><span class='clickable-span'>Hello !</span></div>");
this.collectionBinder = new Backbone.CollectionBinder(elManagerFactory);

this.collection = new Backbone.Collection([1,2,3]);
},

render: function(){
this.collectionBinder.bind(this.collection, this.$el);
return this;
},

onElementClicked: function(event) {
var targetManager = this.collectionBinder.getManagerForEl($(event.currentTarget));
clickCallback(targetManager);

// Stopping propagation (in order to avoid bubbling from span to parent div)
event.stopPropagation();
}
});
return new View();
}

var createViewThenClickAndExpectManagerResolved = function(selectorElementToClick) {
var resolvedManager = undefined;
var clickCallbackCalled = false;
var view = createViewWithClickCallback(function(manager) {
clickCallbackCalled = true;
resolvedManager = manager;
});

view.render();
view.$(selectorElementToClick).click();

expect(clickCallbackCalled).toBe(true);
expect(resolvedManager).toBeTruthy();

view.remove();
};

it('should resolve el manager when span inner element is clicked', function () {
createViewThenClickAndExpectManagerResolved('span.clickable-span:eq(0)');
});

it('should resolve el manager when div root element is clicked', function () {
createViewThenClickAndExpectManagerResolved('div.clickable-div:eq(0)');
});

});