Permalink
Browse files

add setMode

  • Loading branch information...
1 parent 49ea9e9 commit 50ef14d2dc26e5ead1d59e80690319d7257afdd3 @Filirom1 committed Mar 5, 2012
Showing with 34 additions and 0 deletions.
  1. +23 −0 Backbone.setMode.js
  2. 0 README
  3. +11 −0 README.md
View
23 Backbone.setMode.js
@@ -0,0 +1,23 @@
+(function($){
+ // Makes the view enter a mode. Modes have both a 'mode' and a 'group',
+ // and are mutually exclusive with any other modes in the same group.
+ // Setting will update the view's modes hash, as well as set an HTML class
+ // of *[mode]_[group]* on the view's element. Convenient way to swap styles
+ // and behavior.
+ Backbone.View.prototype.setMode = function(mode, group) {
+ this.modes || (this.modes = {});
+ if (this.modes[group] === mode) return;
+ this.$el.setMode(mode, group);
+ this.modes[group] = mode;
+ };
+
+ $.fn.setMode = function(state, group) {
+ group = group || 'mode';
+ var re = new RegExp("\\w+_" + group + "(\\s|$)", 'g');
+ var mode = (state === null) ? "" : state + "_" + group;
+ this.each(function(){
+ this.className = (this.className.replace(re, '') + ' ' + mode).replace(/\s\s/g, ' ');
+ });
+ return mode;
+ };
+})(jQuery || Zepto || Ender);
View
0 README
No changes.
View
11 README.md
@@ -0,0 +1,11 @@
+Backbone.setMode
+================
+
+Backbone plugin: setMode.
+Forked from [DocumentSource](https://github.com/documentcloud/documentcloud/blob/master/public/javascripts/lib/backbone_extensions.js#L8)
+
+Makes the view enter a mode. Modes have both a 'mode' and a 'group',
+and are mutually exclusive with any other modes in the same group.
+Setting will update the view's modes hash, as well as set an HTML class
+of *[mode]_[group]* on the view's element. Convenient way to swap styles
+and behavior.

0 comments on commit 50ef14d

Please sign in to comment.