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

Default serialize implementation (Fix #358) #365

Merged
merged 1 commit into from Sep 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions backbone.layoutmanager.js
Expand Up @@ -901,6 +901,11 @@ LayoutManager.prototype.options = {
return template(context);
},

// By default, pass model attributes to the templates
serialize: function() {
return this.model ? _.clone(this.model.attributes) : {};
},

// This is the most common way you will want to partially apply a view into
// a layout.
partial: function($root, $el, rentManager, manager) {
Expand Down
29 changes: 24 additions & 5 deletions test/spec/configure.js
Expand Up @@ -32,10 +32,6 @@ QUnit.module("configure", {
delete this.Layout.prototype.options.manage;
delete Backbone.View.prototype.manage;

// Remove `serialize` option.
delete this.Layout.prototype.options.serialize;
delete Backbone.View.prototype.serialize;

// Remove `el: false`.
delete this.Layout.prototype.options.el;
delete Backbone.View.prototype.el;
Expand All @@ -54,7 +50,7 @@ QUnit.module("configure", {
});

// Ensure the correct defaults are set for all Layout and View options.
test("defaults", 18, function() {
test("defaults", 19, function() {
// Create a new Layout to test.
var layout = new this.Layout();
// Create a new Layout to test.
Expand Down Expand Up @@ -96,6 +92,8 @@ test("defaults", 18, function() {
ok(_.isFunction(view.options.insert), "View: append is a function");
// The when property should be a function.
ok(_.isFunction(view.options.when), "View: when is a function");
// The serialize property should be a function.
ok(_.isFunction(view.options.serialize), "View: serialize is a function");
});

// Test overriding a single property to ensure propagation works as expected.
Expand Down Expand Up @@ -243,6 +241,27 @@ test("Custom template function", 1, function() {
});
});

asyncTest("Default `serialize` implementation", 2, function() {
var T = Backbone.Layout.extend({
fetchTemplate: function(t) { return t; }
});
var t = new T({
template: _.template("bar")
});
var t2 = new T({
model: new Backbone.Model({ name : "foo" }),
template: _.template("<%= name %>")
});
t.options.when([
t.render().promise().then(function() {
equal(t.$el.html(), "bar", "Should not break rendering if no model is in use");
}),
t2.render().promise().then(function() {
equal(t2.$el.html(), "foo", "Should pass model attributes to the template");
})
]).then(start);
});

// https://github.com/tbranyen/backbone.layoutmanager/issues/201
test("Options passed at instance level overwritten by class level options.", 2, function() {
var layout = new Backbone.Layout();
Expand Down