Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 4761669931d31a4d57d2439eb9813e34625983e7 @nickrivadeneira nickrivadeneira committed Aug 10, 2011
Showing with 93 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +3 −0 README.md
  3. +51 −0 composite_view.js
  4. +17 −0 swapping_router.js
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 thoughtbot
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+# backbone-support
+
+Helper and utility classes that fill out Backbone for serious development.
@@ -0,0 +1,51 @@
+Support.CompositeView = function(options) {
+ this.children = [];
+ Backbone.View.apply(this, [options]);
+};
+
+_.extend(Support.CompositeView.prototype, Backbone.View.prototype, {
+ leave: function() {
+ this.unbind();
+ this.remove();
+ this._leaveChildren();
+ this._removeFromParent();
+ },
+
+ removeChild: function(view) {
+ var index = this.children.indexOf(view);
+ this.children.splice(index, 1);
+ },
+
+ renderChild: function(view) {
+ view.render();
+ this.children.push(view);
+ view.parent = this;
+ },
+
+ appendChild: function(view) {
+ this.renderChild(view);
+ $(this.el).append(view.el);
+ },
+
+ renderChildInto: function(view, container) {
+ this.renderChild(view);
+ $(container).html('').append(view.el);
+ },
+
+ _leaveChildren: function() {
+ var clonedChildren = this.children.slice(0);
+ _.each(clonedChildren, function(view) {
+ if (view.leave) {
+ view.leave();
+ }
+ });
+ },
+
+ _removeFromParent: function() {
+ if (this.parent) {
+ this.parent.removeChild(this);
+ }
+ }
+});
+
+Support.CompositeView.extend = Backbone.View.extend;
@@ -0,0 +1,17 @@
+Support.SwappingRouter = function(options) {
+ Backbone.Router.apply(this, [options]);
+};
+
+_.extend(Support.SwappingRouter.prototype, Backbone.Router.prototype, {
+ swap: function(newView) {
+ if (this.currentView && this.currentView.leave) {
+ this.currentView.leave();
+ }
+
+ this.currentView = newView;
+ this.currentView.render();
+ $(this.el).empty().append(this.currentView.el);
+ }
+});
+
+Support.SwappingRouter.extend = Backbone.Router.extend;

0 comments on commit 4761669

Please sign in to comment.