Fix views.js corner case
This commit is contained in:
parent
2f80ab8f3d
commit
ac2f161271
2 changed files with 34 additions and 3 deletions
|
|
@ -73,7 +73,8 @@ app.views.Base = Backbone.View.extend({
|
||||||
var self = this;
|
var self = this;
|
||||||
_.each(this.subviews, function(property, selector){
|
_.each(this.subviews, function(property, selector){
|
||||||
var view = _.isFunction(self[property]) ? self[property]() : self[property];
|
var view = _.isFunction(self[property]) ? self[property]() : self[property];
|
||||||
if(view) {
|
if (view && self.$(selector).length > 0) {
|
||||||
|
self.$(selector).empty();
|
||||||
self.$(selector).html(view.render().el);
|
self.$(selector).html(view.render().el);
|
||||||
view.delegateEvents();
|
view.delegateEvents();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,36 @@ describe("app.views.Base", function(){
|
||||||
it("renders the sub views from functions", function(){
|
it("renders the sub views from functions", function(){
|
||||||
expect(this.view.$('.subview2').text().trim()).toBe("furreal this is the Second Subview");
|
expect(this.view.$('.subview2').text().trim()).toBe("furreal this is the Second Subview");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context("with nested matching elements", function() {
|
||||||
|
var subviewInstance;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
var counter = 0;
|
||||||
|
var Subview = app.views.Base.extend({
|
||||||
|
templateName: "static-text",
|
||||||
|
|
||||||
|
className: "subview1", // making the internal view's div class match to the external one
|
||||||
|
|
||||||
|
presenter: function() {
|
||||||
|
return {text: "rendered " + ++counter + " times"};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.view.templateName = false; // this is also important specification for the test below
|
||||||
|
this.view.subview1 = function() {
|
||||||
|
subviewInstance = new Subview();
|
||||||
|
return subviewInstance;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it("properly handles nested selectors case", function() {
|
||||||
|
this.view.render();
|
||||||
|
this.view.render();
|
||||||
|
subviewInstance.render();
|
||||||
|
expect(this.view.$(".subview1 .subview1").text()).toBe("rendered 3 times");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context("calling out to third party plugins", function() {
|
context("calling out to third party plugins", function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue