Fix views.js corner case

This commit is contained in:
cmrd Senya 2016-07-14 17:58:11 +03:00
parent 2f80ab8f3d
commit ac2f161271
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
2 changed files with 34 additions and 3 deletions

View file

@ -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();
} }

View file

@ -85,10 +85,40 @@ 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() {
it("replaces .time with relative time ago in words", function(){ it("replaces .time with relative time ago in words", function() {
spyOn($.fn, "timeago"); spyOn($.fn, "timeago");
this.view.render(); this.view.render();
expect($.fn.timeago).toHaveBeenCalled(); expect($.fn.timeago).toHaveBeenCalled();