Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

view and setView are now always available on Backbone.View's, Closes G…

  • Loading branch information...
commit b2ffe48f856a694dbd8c4900470b187e9e8f3914 1 parent 96aec7d
@tbranyen authored
View
8 backbone.layoutmanager.js
@@ -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]) {
@@ -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;
View
2  dist/backbone.layoutmanager.min.js
@@ -3,4 +3,4 @@
* Copyright 2012, Tim Branyen (@tbranyen)
* backbone.layoutmanager.js may be freely distributed under the MIT license.
*/
-(function(a){function e(a){c.each(c.isArray(a)?a:[a],function(a){a.remove(),a.unbind(),a.views&&c.each(a.views,function(a){e(a)}),c.isFunction(a.cleanup)&&a.cleanup.call(a)})}function f(a){function h(c,d){g.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.resolveWith(a,[a.el])}var b,d,e,f=a._options();return{insert:function(b,c){return c?a.view(b,c,!0):a.view("",b,!0)},render:function(i){var j=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&c.isFunction(f.serialize)?i=f.serialize.call(a):!i&&c.isObject(f.serialize)&&(i=f.serialize),e=g.makeAsync(f,c.bind(h,a,i)),c.isString(j)&&(b=a._prefix+j),(d=g.cache(b))?(h(i,d,b),e):(c.isString(j)?d=f.fetch.call(e,a._prefix+j):j!=null&&(d=f.fetch.call(e,j)),e._isAsync||h(i,d),e)}}}"use strict";var b=a.Backbone,c=a._,d=a.$,g=b.View.extend({__manager__:{},constructor:function(a){var d=b.LayoutManager.prototype;a=c.extend({},d.options,a),this._render=function(a){return a(this).render()},this.views={},this.render!==d.render&&(this._render=this.render,this.render=d.render),a.paths&&(this._prefix=a.paths.layout||""),a.views&&this.setViews(a.views),this.template&&(a.template=this.template),b.View.call(this,a)},setViews:function(a){return c.each(a,function(a,b){c.isArray(a)?c.each(a,function(a){this.view(b,a,!0)},this):this.view(b,a)},this),this},view:function(d,f,h){var i,j,k=this;return this.views[d]&&e(this.views[d]),f.__manager__||(f.__manager__={},f instanceof g||(f._render=f.render,f.render===b.View.prototype.render&&(f._render=function(a){return a(this).render()}),f.views&&(f.options.views=f.views),c.extend(f,{views:{},view:g.prototype.view,setViews:g.prototype.setViews,_options:g.prototype._options})),h||(f.__manager__.isManaged=!0),f.render=function(b){function i(a){h||(j.detach(f.el),j.partial(k.el,d,f.el)),c.isFunction(b)&&b.call(f,f.el)}function l(){f.__manager__.hasRendered||(j.partial(k.el,d,f.el,h),f.delegateEvents(),f.__manager__.hasRendered=!0),e.resolveWith(f,[f.el]).then(i)}var e=j.deferred();if(!f.__manager__.isManaged)return e.resolve(f.el).then(i);try{g.prototype.render.call(f,l)}catch(m){a.setTimeout(function(){g.prototype.render.call(f,l)},0)}return e.promise()},j=f._options(),!f._prefix&&j.paths&&(f._prefix=j.paths.template||""),j.views&&f.setViews(j.views)),h?(i=this.views[d]=this.views[d]||[],i.push(f),f):this.views[d]=f},render:function(a){var b=this,d=this._options(),e=d.deferred();return this._render(f).then(function(){b.__manager__.hasRendered||d.detach(b.el);var a=c.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.__manager__.isManaged=!0,c.render(function(){e(a,b)})}var b;return c.isArray(a)?(b=d.deferred(),e(c.clone(a),function(){b.resolve()}),b.promise()):(a.__manager__.isManaged=!0,a.render())});d.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){b.delegateEvents(),c.isFunction(a)&&a.call(b,b.el)}).promise()},_options:function(){return c.extend({},g.prototype.options,this.options)}},{_cache:{},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){c.isObject(a)&&c.extend(g.prototype.options,a)},makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c}});b.LayoutManager=g,b.LayoutManager.View=b.View,g.prototype.options={paths:{},deferred:function(){return d.Deferred()},fetch:function(a){return c.template(d(a).html())},partial:function(a,b,c,e){var f=b?d(a).find(b):d(a);this[e?"append":"html"](f,c)},html:function(a,b){d(a).html(b)},append:function(a,b){d(a).append(b)},detach:function(a){d(a).detach()},when:function(a){return d.when.apply(null,a)},render:function(a,b){return a(b)}}})(this)
+(function(a){function e(a){c.each(c.isArray(a)?a:[a],function(a){a.remove(),a.unbind(),a.views&&c.each(a.views,function(a){e(a)}),c.isFunction(a.cleanup)&&a.cleanup.call(a)})}function f(a){function h(c,d){g.cache(b,d),d&&f.html(a.el,f.render(d,c)),e.resolveWith(a,[a.el])}var b,d,e,f=a._options();return{insert:function(b,c){return c?a.view(b,c,!0):a.view("",b,!0)},render:function(i){var j=a.template||f.template;return a.serialize&&(f.serialize=a.serialize),!i&&c.isFunction(f.serialize)?i=f.serialize.call(a):!i&&c.isObject(f.serialize)&&(i=f.serialize),e=g.makeAsync(f,c.bind(h,a,i)),c.isString(j)&&(b=a._prefix+j),(d=g.cache(b))?(h(i,d,b),e):(c.isString(j)?d=f.fetch.call(e,a._prefix+j):j!=null&&(d=f.fetch.call(e,j)),e._isAsync||h(i,d),e)}}}"use strict";var b=a.Backbone,c=a._,d=a.$,g=b.View.extend({__manager__:{},constructor:function(a){var d=b.LayoutManager.prototype;a=c.extend({},d.options,a),this._render=function(a){return a(this).render()},this.views={},this.render!==d.render&&(this._render=this.render,this.render=d.render),a.paths&&(this._prefix=a.paths.layout||""),a.views&&this.setViews(a.views),this.template&&(a.template=this.template),b.View.call(this,a)},setViews:function(a){return c.each(a,function(a,b){c.isArray(a)?c.each(a,function(a){this.view(b,a,!0)},this):this.view(b,a)},this),this},view:function(d,f,h){var i,j,k=this;return this.views||(this.views={}),this.views[d]&&e(this.views[d]),f.__manager__||(f.__manager__={},f instanceof g||(f._render=f.render,f.render===b.View.prototype.render&&(f._render=function(a){return a(this).render()}),f.views&&(f.options.views=f.views),c.extend(f,{views:{},view:g.prototype.view,setViews:g.prototype.setViews,_options:g.prototype._options})),h||(f.__manager__.isManaged=!0),f.render=function(b){function i(a){h||(j.detach(f.el),j.partial(k.el,d,f.el)),c.isFunction(b)&&b.call(f,f.el)}function l(){f.__manager__.hasRendered||(j.partial(k.el,d,f.el,h),f.delegateEvents(),f.__manager__.hasRendered=!0),e.resolveWith(f,[f.el]).then(i)}var e=j.deferred();if(!f.__manager__.isManaged)return e.resolve(f.el).then(i);try{g.prototype.render.call(f,l)}catch(m){a.setTimeout(function(){g.prototype.render.call(f,l)},0)}return e.promise()},j=f._options(),!f._prefix&&j.paths&&(f._prefix=j.paths.template||""),j.views&&f.setViews(j.views)),h?(i=this.views[d]=this.views[d]||[],i.push(f),f):this.views[d]=f},render:function(a){var b=this,d=this._options(),e=d.deferred();return this._render(f).then(function(){b.__manager__.hasRendered||d.detach(b.el);var a=c.map(b.views,function(a){function e(a,b){if(!a.length)return b();var c=a.shift();c.__manager__.isManaged=!0,c.render(function(){e(a,b)})}var b;return c.isArray(a)?(b=d.deferred(),e(c.clone(a),function(){b.resolve()}),b.promise()):(a.__manager__.isManaged=!0,a.render())});d.when(a).then(function(){e.resolveWith(b,[b.el])})}),e.then(function(){b.delegateEvents(),c.isFunction(a)&&a.call(b,b.el)}).promise()},_options:function(){return c.extend({},g.prototype.options,this.options)}},{_cache:{},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){c.isObject(a)&&c.extend(g.prototype.options,a)},makeAsync:function(a,b){var c=a.deferred();return c.async=function(){return c._isAsync=!0,b},c}});b.View.prototype.view=g.prototype.view,b.View.prototype.setViews=g.prototype.setViews,b.LayoutManager=g,b.LayoutManager.View=b.View,g.prototype.options={paths:{},deferred:function(){return d.Deferred()},fetch:function(a){return c.template(d(a).html())},partial:function(a,b,c,e){var f=b?d(a).find(b):d(a);this[e?"append":"html"](f,c)},html:function(a,b){d(a).html(b)},append:function(a,b){d(a).append(b)},detach:function(a){d(a).detach()},when:function(a){return d.when.apply(null,a)},render:function(a,b){return a(b)}}})(this)
View
40 test/views.js
@@ -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",
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.