Skip to content

Commit

Permalink
view and setView are now always available on Backbone.View's, Closes
Browse files Browse the repository at this point in the history
…GH-63
  • Loading branch information
tbranyen committed Apr 8, 2012
1 parent 96aec7d commit b2ffe48
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backbone.layoutmanager.js
Expand Up @@ -194,6 +194,11 @@ var LayoutManager = Backbone.View.extend({
var partials, options;
var root = this;

// Ensure a view always has a views object
if (!this.views) {
this.views = {};
}

// Make sure any existing views are completely scrubbed of
// events/properties. Do not run clean on append items.
if (this.views[name]) {
Expand Down Expand Up @@ -436,6 +441,9 @@ var LayoutManager = Backbone.View.extend({
}
});

// Ensure all Views always have access to setView and view
Backbone.View.prototype.view = LayoutManager.prototype.view;
Backbone.View.prototype.setViews = LayoutManager.prototype.setViews;

// Attach to Backbone
Backbone.LayoutManager = LayoutManager;
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone.layoutmanager.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions test/views.js
Expand Up @@ -21,6 +21,23 @@ module("views", {
}
});

// Initialize View
this.InitView = Backbone.View.extend({
template: "#test",

serialize: function() {
return { text: this.msg };
},

initialize: function(opts) {
this.msg = opts.msg;

this.setViews({
".inner-right": new setup.SubView()
});
}
});

this.SubView = Backbone.LayoutManager.View.extend({
template: "#test-sub",

Expand Down Expand Up @@ -255,6 +272,29 @@ asyncTest("using setViews", function() {
});
});

asyncTest("using setViews inside initialize", function() {
expect(2);

var main = new Backbone.LayoutManager({
template: "#main"
});

main.setViews({
".right": new this.InitView({
msg: "Left"
})
});

main.render(function(el) {
var trimmed = $.trim( $(el).find(".inner-right div").html() );

ok(isNode(el), "Contents is a DOM Node");
equal(trimmed, "Right", "Correct render");

start();
});
});

asyncTest("extend layoutmanager", function() {
expect(1);

Expand Down

0 comments on commit b2ffe48

Please sign in to comment.