Skip to content
This repository
Browse code

fix 0.6 example

  • Loading branch information...
commit 1fa5f0a449d26388d7a9619429d5c602af17875c 1 parent 8603096
Tim Branyen authored
25 backbone.layoutmanager.js
@@ -249,7 +249,7 @@ var LayoutManager = Backbone.View.extend({
249 249 // Return the deferred.
250 250 return this.__manager__.renderDeferred;
251 251 }
252   -
  252 +
253 253 // Wait until this View has rendered before dealing with nested Views.
254 254 this._render(LayoutManager._viewRender).fetch.then(function() {
255 255 // Disable the ability for any new sub-views to be added.
@@ -314,6 +314,8 @@ var LayoutManager = Backbone.View.extend({
314 314
315 315 if (root.__manager__.handler) {
316 316 root.__manager__.handler.resolveWith(root, [root.el]);
  317 +
  318 + // Remove the handler, so it's never accidentally referenced.
317 319 delete root.__manager__.handler;
318 320 }
319 321
@@ -322,10 +324,10 @@ var LayoutManager = Backbone.View.extend({
322 324 // is also triggered.
323 325 if (_.isFunction(root.__manager__.callback)) {
324 326 root.__manager__.callback.call(root, root.el);
325   - }
326 327
327   - // Remove the most recent callback.
328   - delete root.__manager__.callback;
  328 + // Remove the most recent callback.
  329 + delete root.__manager__.callback;
  330 + }
329 331
330 332 // Remove the rendered deferred.
331 333 delete root.__manager__.renderDeferred;
@@ -494,10 +496,15 @@ var LayoutManager = Backbone.View.extend({
494 496
495 497 // Configure a View to work with the LayoutManager plugin.
496 498 setupView: function(view, options) {
497   - var views;
  499 + var views, viewOptions;
498 500 var proto = Backbone.LayoutManager.prototype;
499 501 var viewOverrides = _.pick(view, keys);
500 502
  503 + // If the View has already been setup, no need to do it again.
  504 + if (view.__manager__) {
  505 + return;
  506 + }
  507 +
501 508 // Ensure necessary properties are set.
502 509 _.defaults(view, {
503 510 // Ensure a view always has a views object.
@@ -518,6 +525,10 @@ var LayoutManager = Backbone.View.extend({
518 525 options = view.options = _.defaults(options || {}, view.options,
519 526 proto.options);
520 527
  528 + // Ensure view events are properly copied over.
  529 + viewOptions = _.pick(options, ["events"].concat(_.values(options.events)));
  530 + _.extend(view, viewOptions);
  531 +
521 532 // If the View still has the Backbone.View#render method, remove it. Don't
522 533 // want it accidentally overriding the LM render.
523 534 delete viewOverrides.render;
@@ -599,8 +610,8 @@ var LayoutManager = Backbone.View.extend({
599 610 // options.
600 611 views = options.views || view.views;
601 612
602   - // Set the internal views.
603   - if (views) {
  613 + // Set the internal views, only if selectors have been provided.
  614 + if (_.keys(views).length) {
604 615 view.setViews(views);
605 616 }
606 617
2  dist/backbone.layoutmanager.min.js
@@ -3,4 +3,4 @@
3 3 * Copyright 2012, Tim Branyen (@tbranyen)
4 4 * backbone.layoutmanager.js may be freely distributed under the MIT license.
5 5 */
6   -(function(a){"use strict";var b,c=a.Backbone,d=a._,e=a.$,f=c.View.prototype._configure,g=c.View.prototype.render,h=c.View.extend({constructor:function(a){a=a||{},h.setupView(this,a),c.View.call(this,a),this.__manager__.prefix=this._options().paths.layout||""},swapLayout:function(a){return a.views=d.defaults({},this.views,a.views),a.setElement(this.el),a},insertView:function(a,b){return b?this.setView(a,b,!0):this.setView(a,!0)},insertViews:function(a){return d.each(a,function(b,c){a[c]=[].concat(b)}),this.setViews(a)},getView:function(a){return this.getViews(a).first().value()},getViews:function(a){var b=d.chain(this.views).map(function(a){return[].concat(a)},this).flatten().value();return d.chain(d.filter(b,a?a:d.identity))},setView:function(a,b,c){var e,f,g=this;return d.isString(a)||(c=b,b=a,a=""),this.views=this.views||{},!c&&this.views[a]&&(d.isArray(this.views[a])?d.each(this.views[a],function(a){a.remove()}):this.views[a].remove()),f=b._options(),b.__manager__||h.setupView(b,f),b.render=function(e){function j(){(!c||!b.__manager__.hasRendered)&&f.partial(g.el,a,b.el,c)&&(b.__manager__.hasRendered=!0),b.delegateEvents(),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),i.resolveWith(b,[b.el]),d.isFunction(e)&&e.call(b,b.el)}var i=f.deferred();return b._removeView(),h.prototype.render.call(b).then(j),i.promise()},!b.__manager__.prefix&&f.paths&&(b.__manager__.prefix=f.paths.template||""),b.__manager__.parent=this,b.__manager__.selector=a,c?(e=this.views[a]=this.views[a]||[],d.isArray(this.views[a])||(e=this.views[a]=[this.views[a]]),d.indexOf(e,b)>-1?b:(e.push(b),b.__manager__.append=!0,b)):this.views[a]=b},setViews:function(a){return d.each(a,function(a,b){if(d.isArray(a))return d.each(a,function(a){this.insertView(b,a)},this);this.setView(b,a)},this),this},render:function(a){var b=this,c=this._options(),e=c.deferred();return this.__manager__.renderDeferred?(this.__manager__.callback=a,this.__manager__.renderDeferred):(this._render(h._viewRender).fetch.then(function(){b.__manager__.renderDeferred=e;var a=d.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.render(function(){e(a,b)})}var b;return d.isArray(a)?(b=c.deferred(),e(d.clone(a),function(){b.resolve()}),b.promise()):a.render()});c.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){d.isFunction(a)&&a.call(b,b.el),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),d.isFunction(b.__manager__.callback)&&b.__manager__.callback.call(b,b.el),delete b.__manager__.callback,delete b.__manager__.renderDeferred}))},remove:function(){return h.cleanViews(this),this._remove.apply(this,arguments)},_options:function(){return d.extend({},this,h.prototype.options,this.options)}},{_cache:{},_makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c},_viewRender:function(a){function g(c,d){h.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.fetch.resolveWith(a,[a.el])}var b,c,e,f=a._options();return{render:function(i){var j=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&d.isFunction(f.serialize)?i=f.serialize.call(a):!i&&d.isObject(f.serialize)&&(i=f.serialize),e=h._makeAsync(f,d.bind(g,a,i)),e.fetch=f.deferred(),a.__manager__.handler=e,d.isString(j)&&(b=a.__manager__.prefix+j),(c=h.cache(b))?(g(i,c,b),e):(d.isString(j)?c=f.fetch.call(e,a.__manager__.prefix+j):j!=null&&(c=f.fetch.call(e,j)),e._isAsync||g(i,c),e)}}},cleanViews:function(a){d.each([].concat(a),function(a){a.unbind(),a.views&&d.each(a.views,function(a){h.cleanViews(a)}),d.isFunction(a.cleanup)&&a.cleanup.call(a)})},cache:function(a,b){if(a in this._cache)return this._cache[a];if(a!=null&&b!=null)return this._cache[a]=b},configure:function(a){d.extend(h.prototype.options,a),a.manage&&(c.View.prototype.manage=!0)},setupView:function(a,e){var f,g=c.LayoutManager.prototype,i=d.pick(a,b);d.defaults(a,{views:{},__manager__:{},_options:h.prototype._options,_removeView:h._removeView}),e=a.options=d.defaults(e||{},a.options,g.options),delete i.render,d.extend(e,i),a._remove=c.View.prototype.remove,a._render=function(a){var b,c=this._options().beforeRender,e=this._options().afterRender;return d.isFunction(c)&&c.call(this,this),this.trigger("beforeRender",this),b=a(this).render(),b.then(function(){var a=this.__manager__.parent,b=this,c=function(){this.delegateEvents(),d.isFunction(e)&&e.call(this,this),this.trigger("afterRender",this)};if(!a||!a.__manager__.handler)return c.call(this);a.__manager__.handler.then(function(){c.call(b)})}),b},a.render=h.prototype.render,a.remove!==g.remove&&(a._remove=a.remove,a.remove=g.remove),a.__manager__.prefix="",f=e.views||a.views,f&&a.setViews(f),a.template&&(e.template=a.template,delete a.template)},_removeView:function(a){a=a||this,a.getViews().each(function(a){var b=a.__manager__,c=d.isBoolean(a.keep)?a.keep:a.options.keep;if(!c&&a.__manager__.append===!0){a.remove();if(d.isArray(b.parent.views[b.selector]))return b.parent.getView(function(a,c){a.__manager__.selector===b.selector&&b.parent.views[b.selector].splice(c,1)});delete b.parent[b.selector]}})}});d.each(["get","set","insert"],function(a){var b=c.View.prototype,d=h.prototype;b[a+"View"]=d[a+"View"],b[a+"Views"]=d[a+"Views"]}),c.Layout=c.LayoutManager=h,c.LayoutView=c.View.extend({manage:!0}),c.View.prototype._configure=function(){var a=f.apply(this,arguments);return this.manage&&h.setupView(this),a},h.prototype.options={paths:{},deferred:function(){return e.Deferred()},fetch:function(a){return d.template(e(a).html())},partial:function(a,b,c,d){var f=b?e(a).find(b):e(a);return f.length?(this[d?"append":"html"](f,c),!0):!1},html:function(a,b){e(a).html(b)},append:function(a,b){e(a).append(b)},when:function(a){return e.when.apply(null,a)},render:function(a,b){return a(b)}},b=d.keys(h.prototype.options)})(this);
  6 +(function(a){"use strict";var b,c=a.Backbone,d=a._,e=a.$,f=c.View.prototype._configure,g=c.View.prototype.render,h=c.View.extend({constructor:function(a){a=a||{},h.setupView(this,a),c.View.call(this,a),this.__manager__.prefix=this._options().paths.layout||""},swapLayout:function(a){return a.views=d.defaults({},this.views,a.views),a.setElement(this.el),a},insertView:function(a,b){return b?this.setView(a,b,!0):this.setView(a,!0)},insertViews:function(a){return d.each(a,function(b,c){a[c]=[].concat(b)}),this.setViews(a)},getView:function(a){return this.getViews(a).first().value()},getViews:function(a){var b=d.chain(this.views).map(function(a){return[].concat(a)},this).flatten().value();return d.chain(d.filter(b,a?a:d.identity))},setView:function(a,b,c){var e,f,g=this;return d.isString(a)||(c=b,b=a,a=""),this.views=this.views||{},!c&&this.views[a]&&(d.isArray(this.views[a])?d.each(this.views[a],function(a){a.remove()}):this.views[a].remove()),f=b._options(),b.__manager__||h.setupView(b,f),b.render=function(e){function j(){(!c||!b.__manager__.hasRendered)&&f.partial(g.el,a,b.el,c)&&(b.__manager__.hasRendered=!0),b.delegateEvents(),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),i.resolveWith(b,[b.el]),d.isFunction(e)&&e.call(b,b.el)}var i=f.deferred();return b._removeView(),h.prototype.render.call(b).then(j),i.promise()},!b.__manager__.prefix&&f.paths&&(b.__manager__.prefix=f.paths.template||""),b.__manager__.parent=this,b.__manager__.selector=a,c?(e=this.views[a]=this.views[a]||[],d.isArray(this.views[a])||(e=this.views[a]=[this.views[a]]),d.indexOf(e,b)>-1?b:(e.push(b),b.__manager__.append=!0,b)):this.views[a]=b},setViews:function(a){return d.each(a,function(a,b){if(d.isArray(a))return d.each(a,function(a){this.insertView(b,a)},this);this.setView(b,a)},this),this},render:function(a){var b=this,c=this._options(),e=c.deferred();return this.__manager__.renderDeferred?(this.__manager__.callback=a,this.__manager__.renderDeferred):(this._render(h._viewRender).fetch.then(function(){b.__manager__.renderDeferred=e;var a=d.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.render(function(){e(a,b)})}var b;return d.isArray(a)?(b=c.deferred(),e(d.clone(a),function(){b.resolve()}),b.promise()):a.render()});c.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){d.isFunction(a)&&a.call(b,b.el),b.__manager__.handler&&(b.__manager__.handler.resolveWith(b,[b.el]),delete b.__manager__.handler),d.isFunction(b.__manager__.callback)&&(b.__manager__.callback.call(b,b.el),delete b.__manager__.callback),delete b.__manager__.renderDeferred}))},remove:function(){return h.cleanViews(this),this._remove.apply(this,arguments)},_options:function(){return d.extend({},this,h.prototype.options,this.options)}},{_cache:{},_makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c},_viewRender:function(a){function g(c,d){h.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.fetch.resolveWith(a,[a.el])}var b,c,e,f=a._options();return{render:function(i){var j=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&d.isFunction(f.serialize)?i=f.serialize.call(a):!i&&d.isObject(f.serialize)&&(i=f.serialize),e=h._makeAsync(f,d.bind(g,a,i)),e.fetch=f.deferred(),a.__manager__.handler=e,d.isString(j)&&(b=a.__manager__.prefix+j),(c=h.cache(b))?(g(i,c,b),e):(d.isString(j)?c=f.fetch.call(e,a.__manager__.prefix+j):j!=null&&(c=f.fetch.call(e,j)),e._isAsync||g(i,c),e)}}},cleanViews:function(a){d.each([].concat(a),function(a){a.unbind(),a.views&&d.each(a.views,function(a){h.cleanViews(a)}),d.isFunction(a.cleanup)&&a.cleanup.call(a)})},cache:function(a,b){if(a in this._cache)return this._cache[a];if(a!=null&&b!=null)return this._cache[a]=b},configure:function(a){d.extend(h.prototype.options,a),a.manage&&(c.View.prototype.manage=!0)},setupView:function(a,e){var f,g,i=c.LayoutManager.prototype,j=d.pick(a,b);if(a.__manager__)return;d.defaults(a,{views:{},__manager__:{},_options:h.prototype._options,_removeView:h._removeView}),e=a.options=d.defaults(e||{},a.options,i.options),g=d.pick(e,["events"].concat(d.values(e.events))),d.extend(a,g),delete j.render,d.extend(e,j),a._remove=c.View.prototype.remove,a._render=function(a){var b,c=this._options().beforeRender,e=this._options().afterRender;return d.isFunction(c)&&c.call(this,this),this.trigger("beforeRender",this),b=a(this).render(),b.then(function(){var a=this.__manager__.parent,b=this,c=function(){this.delegateEvents(),d.isFunction(e)&&e.call(this,this),this.trigger("afterRender",this)};if(!a||!a.__manager__.handler)return c.call(this);a.__manager__.handler.then(function(){c.call(b)})}),b},a.render=h.prototype.render,a.remove!==i.remove&&(a._remove=a.remove,a.remove=i.remove),a.__manager__.prefix="",f=e.views||a.views,d.keys(f).length&&a.setViews(f),a.template&&(e.template=a.template,delete a.template)},_removeView:function(a){a=a||this,a.getViews().each(function(a){var b=a.__manager__,c=d.isBoolean(a.keep)?a.keep:a.options.keep;if(!c&&a.__manager__.append===!0){a.remove();if(d.isArray(b.parent.views[b.selector]))return b.parent.getView(function(a,c){a.__manager__.selector===b.selector&&b.parent.views[b.selector].splice(c,1)});delete b.parent[b.selector]}})}});d.each(["get","set","insert"],function(a){var b=c.View.prototype,d=h.prototype;b[a+"View"]=d[a+"View"],b[a+"Views"]=d[a+"Views"]}),c.Layout=c.LayoutManager=h,c.LayoutView=c.View.extend({manage:!0}),c.View.prototype._configure=function(){var a=f.apply(this,arguments);return this.manage&&h.setupView(this),a},h.prototype.options={paths:{},deferred:function(){return e.Deferred()},fetch:function(a){return d.template(e(a).html())},partial:function(a,b,c,d){var f=b?e(a).find(b):e(a);return f.length?(this[d?"append":"html"](f,c),!0):!1},html:function(a,b){e(a).html(b)},append:function(a,b){e(a).append(b)},when:function(a){return e.when.apply(null,a)},render:function(a,b){return a(b)}},b=d.keys(h.prototype.options)})(this);
2  examples/0.6/swap-layout.html
@@ -57,7 +57,6 @@
57 57 },
58 58
59 59 swap: function(ev) {
60   - console.log("woot");
61 60 this.swapLayout(goodbye).render();
62 61 }
63 62 });
@@ -71,7 +70,6 @@
71 70 },
72 71
73 72 swap: function(ev) {
74   - console.log("here");
75 73 this.swapLayout(hello).render();
76 74 }
77 75 });
1  test/vendor/backbone.js
@@ -1224,6 +1224,7 @@
1224 1224 delegateEvents: function(events) {
1225 1225 if (!(events || (events = getValue(this, 'events')))) return;
1226 1226 this.undelegateEvents();
  1227 +
1227 1228 for (var key in events) {
1228 1229 var method = events[key];
1229 1230 if (!_.isFunction(method)) method = this[events[key]];
40 test/views.js
@@ -991,3 +991,43 @@ test("render works when assigned early", 1, function() {
991 991
992 992 ok(hit, "render works as expected when assigned early");
993 993 });
  994 +
  995 +test("Render doesn't work inside insertView", 1, function() {
  996 + var V = Backbone.LayoutView.extend({
  997 + template: "<p class='inner'><%= lol %></p>",
  998 + fetch: function(path) { return _.template(path); }
  999 + });
  1000 +
  1001 + var n = new Backbone.LayoutView({
  1002 + template: "<p></p>",
  1003 + fetch: function(path) { return _.template(path); }
  1004 + });
  1005 +
  1006 + n.render();
  1007 + n.insertView("p", new V({ serialize: { lol: "hi" } })).render();
  1008 +
  1009 + equal(n.$("p.inner").html(), "hi", "Render works with insertView");
  1010 +});
  1011 +
  1012 +// https://github.com/tbranyen/backbone.layoutmanager/issues/134
  1013 +test("Ensure events are copied over properly", 1, function() {
  1014 + var hit = false;
  1015 + var layout = new Backbone.Layout({
  1016 + template: "<p></p>",
  1017 + fetch: function(path) { return _.template(path); },
  1018 +
  1019 + events: {
  1020 + "click p": "test"
  1021 + },
  1022 +
  1023 + test: function(ev) {
  1024 + hit = true;
  1025 + }
  1026 + });
  1027 +
  1028 + layout.render();
  1029 +
  1030 + layout.$("p").click();
  1031 +
  1032 + ok(hit, "Events were bound and triggered correctly");
  1033 +});

0 comments on commit 1fa5f0a

Please sign in to comment.
Something went wrong with that request. Please try again.