diff --git a/backbone-fiber.js b/backbone-fiber.js index 3b6e66f..dab710e 100644 --- a/backbone-fiber.js +++ b/backbone-fiber.js @@ -146,22 +146,29 @@ * relationship and renders the view. */ function create( view, $el, options ) { - + var inst = new view(_.extend( options, { el: $el[0] } )), - parent; + parent, fizzle = false; $el.attr( 'data-cid', inst.cid ); _view_inst[inst.cid] = inst; parent = $el.parents( '[data-view]' ).first(); + if ( parent.length > 0 ) { parent = parent.attr( 'data-cid' ); - _view_inst[parent].addChild( inst ); - inst.setParent( _view_inst[parent] ); + if ( _view_inst[parent] ) { + _view_inst[parent].addChild( inst ); + inst.setParent( _view_inst[parent] ); + } else { + fizzle = true; + inst.remove(); + } } - inst.render(); + if (!fizzle) { inst.render(); } return inst; + } /** @@ -219,6 +226,13 @@ */ renderedOnce: false, + /** + * Flag to force the view to render even if it has an empty collection + * and has not renderedOnce + */ + forceRender: false, + + /** * Enables dynamically adding new DOM elements that will be * bound to a child view instance. @@ -367,7 +381,7 @@ data = this.dataSerialized(); isa = data && $.isArray( data ); - if ( this.renderedOnce && data || ( !this.renderedOnce && ( ( isa && data.length > 0 ) || !isa ) ) ) + if ( ( this.forceRender || this.renderedOnce ) && data || ( !this.renderedOnce && ( ( isa && data.length > 0 ) || !isa ) ) ) { this.$el.empty().html( this.template( data ) ); this.renderedOnce = true; @@ -427,7 +441,7 @@ this.bindData(); - if (dm && dm.trigger) {dm.trigger('ready', dm);} + if (dm && dm.trigger) { dm.trigger( 'ready', dm, this );} return this; }, @@ -534,7 +548,7 @@ _.compact( _.map( this.children, function( cid ) { var view; - if ( (view = Fiber.getViewFromCid( this.children[i] )) && view.instanceOf == type ) + if ( (view = Fiber.getViewFromCid( cid )) && view.instanceOf == type ) return view; }) ) @@ -570,6 +584,16 @@ } return view; + }, + + /** + * Determines if an element is in my view and not a child's view. + */ + isMyElement: function( el ) { + var $el = (el instanceof $ ? el : $(el)); + + return $el.parents('[data-view]').first().attr('data-cid') == this.cid; + } }]); diff --git a/backbone-fiber.min.js b/backbone-fiber.min.js index fa2c818..89a7995 100644 --- a/backbone-fiber.min.js +++ b/backbone-fiber.min.js @@ -1 +1 @@ -(function(t,e){"function"==typeof define&&define.amd?define(["jquery","underscore","backbone","base"],e):t.Backbone.Fiber=e(t.$,t._,t.Backbone,Base)})(this,function(t,e,n,r){function o(i){var n=t.Deferred();return c[i]?n.resolve(c[i]):(l[i]=n,require([s.viewPath+i,"text!"+s.viewPath+i+".html"],function(t,r){t.prototype.instanceOf=i,t.prototype.template=e.template(r),c[i]=t,delete l[i],n.resolve(t)})),n.promise()}function a(t,i,n){var r,o=new t(e.extend(n,{el:i[0]}));return i.attr("data-cid",o.cid),h[o.cid]=o,r=i.parents("[data-view]").first(),r.length>0&&(r=r.attr("data-cid"),h[r].addChild(o),o.setParent(h[r])),o.render(),o}function d(e,i){var n=i||{},r=e instanceof t?e:t(e),d=r.attr("data-view");d&&o(d).done(function(t){a(t,r,n)})}n.View.extend=r.extend,n.View.mix=r.mix;var s,c={},h={},l={},u=t.cleanData;return t.cleanData=function(e){for(var i,n=0;void 0!==(i=e[n]);n++)t(i).triggerHandler("destroyed");u(e)},n.Fiber=s={viewPath:"views/",getViewFromEl:function(e){var i=t(e),n=i.attr("data-cid")||i.closest("[data-cid]").attr("data-cid");return n?h[n]:void 0},getViewFromCid:function(t){return h[t]},getElFromCid:function(t){return h[t]?h[t].$el:void 0},getPromise:function(t){return l[t]?l[t]:void 0}},s.start=function(){d(document.body)},n.View.mix([{template:null,parent:null,children:null,renderedOnce:!1,connect:function(e,i){var n,r,o=this,a=t.Deferred();return n="string"==typeof e?this.factory(e):e instanceof t?e:t(e),d(n,i),(r=s.getPromise(n.attr("data-view")))?r.done(function(){a.resolveWith(o,[s.getViewFromEl(n)])}):a.resolveWith(this,[s.getViewFromEl(n)]),a.promise()},waitFor:function(i,n){var r=this,o=e.compact(e.map(i,function(t){return s.getPromise(t)}));t.when.apply(this,o).done(function(){n.apply(r,arguments)})},factory:function(e){return t("
").attr("data-view",e).appendTo(this.$el)},initialize:function(t){this.$el.on("destroyed",e.bind(this.remove,this)),this.options=t||{},this.children=[],this.setup(t),this.bindData(),this.trigger("created")},unbindData:function(){var t=this.data();t.trigger&&this.stopListening(t)},bindData:function(){var i=this,n=this.data();n.trigger&&e.each(this.events,function(e,r){var o;(o=r.indexOf(".data"))>-1&&(e=t.isFunction(e)?e:i[e],i.listenTo(n,r.slice(0,o),e))})},setup:t.noop,render:function(){var e,i;this.beforeRender()!==!1&&(this.trigger("rendering"),this.template&&"function"==typeof this.template&&(e=this.dataSerialized(),i=e&&t.isArray(e),(this.renderedOnce&&e||!this.renderedOnce&&(i&&e.length>0||!i))&&(this.$el.empty().html(this.template(e)),this.renderedOnce=!0,this.$el.find("[data-view]").each(function(){d(this)}))),this.afterRender(),this.trigger("rendered"))},beforeRender:t.noop,afterRender:t.noop,data:function(){return this.model?this.model:this.collection?this.collection:{}},setData:function(t){var e=null;return this.unbindData(),t.model?this.model=e=t.model:t.collection&&(this.collection=e=t.collection),this.bindData(),e&&e.trigger&&e.trigger("ready",e),this},dataSerialized:function(){var t=this.data();return t.toJSON?t.toJSON({computedFields:!0}):t},remove:function(){this.parent?h[this.parent].removeChild(this):(this.destroy(),this.trigger("removed"),this.stopListening(),this.$el.off("destroyed"),this.$el.remove(),delete h[this.cid]),this._superStop()},destroy:t.noop,trigger:function(t,e){this.$el.trigger(t,{view:this,data:e||{}})||this._superStop()},setParent:function(t){this.parent=null,t&&(this.parent=t.cid)},addChild:function(t){e.contains(t.cid,this.children)||this.children.push(t.cid)},removeChild:function(t){var i=e.indexOf(this.children,t.cid);i>-1&&(this.children.splice(i,1),t.setParent(null),t.remove())},allChildren:function(){return e.map(this.children,function(t){return s.getViewFromCid(t)})},findChildren:function(t){return e.compact(e.map(this.children,function(){var e;return(e=s.getViewFromCid(this.children[i]))&&e.instanceOf==t?e:void 0}))},findChild:function(e){var i=null;if(e instanceof t)i=s.getViewFromEl(e);else if("string"==typeof e&&(i=s.getViewFromCid(e),!i))for(var n=0;this.children.length>n;n++){var r;(r=s.getViewFromCid(this.children[n]))&&r.instanceOf==e&&(i=r)}return i}}]),s}); \ No newline at end of file +(function(t,e){"function"==typeof define&&define.amd?define(["jquery","underscore","backbone","base"],e):t.Backbone.Fiber=e(t.$,t._,t.Backbone,Base)})(this,function(t,e,i,n){function r(i){var n=t.Deferred();return s[i]?n.resolve(s[i]):(h[i]=n,require([d.viewPath+i,"text!"+d.viewPath+i+".html"],function(t,r){t.prototype.instanceOf=i,t.prototype.template=e.template(r),s[i]=t,delete h[i],n.resolve(t)})),n.promise()}function o(t,i,n){var r,o=new t(e.extend(n,{el:i[0]})),a=!1;return i.attr("data-cid",o.cid),c[o.cid]=o,r=i.parents("[data-view]").first(),r.length>0&&(r=r.attr("data-cid"),c[r]?(c[r].addChild(o),o.setParent(c[r])):(a=!0,o.remove())),a||o.render(),o}function a(e,i){var n=i||{},a=e instanceof t?e:t(e),d=a.attr("data-view");d&&r(d).done(function(t){o(t,a,n)})}i.View.extend=n.extend,i.View.mix=n.mix;var d,s={},c={},h={},l=t.cleanData;return t.cleanData=function(e){for(var i,n=0;void 0!==(i=e[n]);n++)t(i).triggerHandler("destroyed");l(e)},i.Fiber=d={viewPath:"views/",getViewFromEl:function(e){var i=t(e),n=i.attr("data-cid")||i.closest("[data-cid]").attr("data-cid");return n?c[n]:void 0},getViewFromCid:function(t){return c[t]},getElFromCid:function(t){return c[t]?c[t].$el:void 0},getPromise:function(t){return h[t]?h[t]:void 0}},d.start=function(){a(document.body)},i.View.mix([{template:null,parent:null,children:null,renderedOnce:!1,forceRender:!1,connect:function(e,i){var n,r,o=this,s=t.Deferred();return n="string"==typeof e?this.factory(e):e instanceof t?e:t(e),a(n,i),(r=d.getPromise(n.attr("data-view")))?r.done(function(){s.resolveWith(o,[d.getViewFromEl(n)])}):s.resolveWith(this,[d.getViewFromEl(n)]),s.promise()},waitFor:function(i,n){var r=this,o=e.compact(e.map(i,function(t){return d.getPromise(t)}));t.when.apply(this,o).done(function(){n.apply(r,arguments)})},factory:function(e){return t("
").attr("data-view",e).appendTo(this.$el)},initialize:function(t){this.$el.on("destroyed",e.bind(this.remove,this)),this.options=t||{},this.children=[],this.setup(t),this.bindData(),this.trigger("created")},unbindData:function(){var t=this.data();t.trigger&&this.stopListening(t)},bindData:function(){var i=this,n=this.data();n.trigger&&e.each(this.events,function(e,r){var o;(o=r.indexOf(".data"))>-1&&(e=t.isFunction(e)?e:i[e],i.listenTo(n,r.slice(0,o),e))})},setup:t.noop,render:function(){var e,i;this.beforeRender()!==!1&&(this.trigger("rendering"),this.template&&"function"==typeof this.template&&(e=this.dataSerialized(),i=e&&t.isArray(e),((this.forceRender||this.renderedOnce)&&e||!this.renderedOnce&&(i&&e.length>0||!i))&&(this.$el.empty().html(this.template(e)),this.renderedOnce=!0,this.$el.find("[data-view]").each(function(){a(this)}))),this.afterRender(),this.trigger("rendered"))},beforeRender:t.noop,afterRender:t.noop,data:function(){return this.model?this.model:this.collection?this.collection:{}},setData:function(t){var e=null;return this.unbindData(),t.model?this.model=e=t.model:t.collection&&(this.collection=e=t.collection),this.bindData(),e&&e.trigger&&e.trigger("ready",e,this),this},dataSerialized:function(){var t=this.data();return t.toJSON?t.toJSON({computedFields:!0}):t},remove:function(){this.parent?c[this.parent].removeChild(this):(this.destroy(),this.trigger("removed"),this.stopListening(),this.$el.off("destroyed"),this.$el.remove(),delete c[this.cid]),this._superStop()},destroy:t.noop,trigger:function(t,e){this.$el.trigger(t,{view:this,data:e||{}})||this._superStop()},setParent:function(t){this.parent=null,t&&(this.parent=t.cid)},addChild:function(t){e.contains(t.cid,this.children)||this.children.push(t.cid)},removeChild:function(t){var i=e.indexOf(this.children,t.cid);i>-1&&(this.children.splice(i,1),t.setParent(null),t.remove())},allChildren:function(){return e.map(this.children,function(t){return d.getViewFromCid(t)})},findChildren:function(t){return e.compact(e.map(this.children,function(e){var i;return(i=d.getViewFromCid(e))&&i.instanceOf==t?i:void 0}))},findChild:function(e){var i=null;if(e instanceof t)i=d.getViewFromEl(e);else if("string"==typeof e&&(i=d.getViewFromCid(e),!i))for(var n=0;this.children.length>n;n++){var r;(r=d.getViewFromCid(this.children[n]))&&r.instanceOf==e&&(i=r)}return i},isMyElement:function(e){var i=e instanceof t?e:t(e);return i.parents("[data-view]").first().attr("data-cid")==this.cid}}]),d}); \ No newline at end of file diff --git a/bower.json b/bower.json index 0c35931..d00493b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "backbone-fiber", - "version": "0.1.2", + "version": "0.1.3", "ignore": [ "bower_components", "node_modules", diff --git a/package.json b/package.json index e25200b..143de5d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "Backbone.Fiber", "title": "", "description": "", - "version": "0.1.2", + "version": "0.1.3", "author": { "name": "Ben Olson", "url": "http://bseth99.github.com/"