Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 4.86 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Component extends Backbone.View
  2.  
  3.   defaults:
  4.     'visible': true
  5.  
  6.   initialize: ->
  7.     # defaults gets overriden by @config, which is overridden
  8.     # by the passed in options on initialization. So @config
  9.     # acts as @defaults for something that extends Component
  10.     @options   = _.extend({}, @defaults, @config, @options)
  11.     for func, args of @options
  12.       if @[func]?
  13.         console.log(func)
  14.         if _.isArray(args)
  15.           @[func].apply?(@, args)
  16.         else
  17.           @[func].call?(@, args)
  18.       null
  19.  
  20.   render: =>
  21.     @
  22.  
  23.   class: (klass) =>
  24.     return @ unless klass?
  25.     @$el.addClass(klass)
  26.     @
  27.  
  28.   css: (css) =>
  29.     return @ unless css?
  30.     @$el.css(css)
  31.     @
  32.  
  33.   visible: (show) =>
  34.     if show then @show() else @hide()
  35.     @
  36.  
  37.   show: =>
  38.     @render()
  39.     @delegateEvents(@events)
  40.     @$el.removeClass('hide')
  41.     @_visible = true
  42.     @
  43.  
  44.   hide: =>
  45.     @$el.addClass('hide')
  46.     @undelegateEvents()
  47.     @_visible = false
  48.     @
  49.  
  50.  
  51. class Menu extends Component
  52.  
  53.   tagName: 'ul'
  54.  
  55.   config:
  56.     'bar': 'testing'
  57.     'class': 'menu'
  58.     'visible': false
  59.  
  60.   initialize: ->
  61.     super()
  62.  
  63.   bar: (string) =>
  64.    console.log(string)
  65.    @
  66.  
  67.  
  68.  
  69. menu = new Menu({
  70.   'class': 'test-menu',
  71.   'css': {
  72.     'width': '300px',
  73.     'background-color': 'blue'
  74.   }
  75. })
  76.  
  77.  
  78.  
  79. # Result:
  80. # ---------------
  81. # menu.el:
  82. #   <ul class='menu test-menu hide' style='width: 300px; background-color: blue;'></ul>
  83. #
  84. # and the console will log out:
  85. #   calling func: visible with args: false
  86. #   calling func: bar with args: testing
  87. #   testing
  88. #   calling func: class with args: test-menu
  89. #   calling func: css with args: [object Object]
  90.  
  91.  
  92.  
  93. # Go to http://documentcloud.github.com/backbone/ and open
  94. # the Console and Copy and Paste the following compiled
  95. # CoffeeScript to try it out
  96. # ----------------------------------------------------------
  97. var Component, Menu,
  98.   __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  99.   __hasProp = Object.prototype.hasOwnProperty,
  100.   __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
  101.  
  102. Component = (function(_super) {
  103.  
  104.   __extends(Component, _super);
  105.  
  106.   function Component() {
  107.     this.hide = __bind(this.hide, this);
  108.     this.show = __bind(this.show, this);
  109.     this.visible = __bind(this.visible, this);
  110.     this.css = __bind(this.css, this);
  111.     this["class"] = __bind(this["class"], this);
  112.     this.render = __bind(this.render, this);
  113.     Component.__super__.constructor.apply(this, arguments);
  114.   }
  115.  
  116.   Component.prototype.defaults = {
  117.     'visible': true
  118.   };
  119.  
  120.   Component.prototype.initialize = function() {
  121.     var args, func, _base, _base2, _ref, _results;
  122.     this.options = _.extend({}, this.defaults, this.config, this.options);
  123.     _ref = this.options;
  124.     _results = [];
  125.     for (func in _ref) {
  126.       args = _ref[func];
  127.       if (this[func] != null) {
  128.         console.log("calling func: " + func + " with args: " + args);
  129.         if (_.isArray(args)) {
  130.           if (typeof (_base = this[func]).apply === "function") {
  131.             _base.apply(this, args);
  132.           }
  133.         } else {
  134.           if (typeof (_base2 = this[func]).call === "function") {
  135.             _base2.call(this, args);
  136.           }
  137.         }
  138.       }
  139.       _results.push(null);
  140.     }
  141.     return _results;
  142.   };
  143.  
  144.   Component.prototype.render = function() {
  145.     return this;
  146.   };
  147.  
  148.   Component.prototype["class"] = function(klass) {
  149.     if (klass == null) return this;
  150.     this.$el.addClass(klass);
  151.     return this;
  152.   };
  153.  
  154.   Component.prototype.css = function(css) {
  155.     if (css == null) return this;
  156.     this.$el.css(css);
  157.     return this;
  158.   };
  159.  
  160.   Component.prototype.visible = function(show) {
  161.     if (show) {
  162.       this.show();
  163.     } else {
  164.       this.hide();
  165.     }
  166.     return this;
  167.   };
  168.  
  169.   Component.prototype.show = function() {
  170.     this.render();
  171.     this.delegateEvents(this.events);
  172.     this.$el.removeClass('hide');
  173.     this._visible = true;
  174.     return this;
  175.   };
  176.  
  177.   Component.prototype.hide = function() {
  178.     this.$el.addClass('hide');
  179.     this.undelegateEvents();
  180.     this._visible = false;
  181.     return this;
  182.   };
  183.  
  184.   return Component;
  185.  
  186. })(Backbone.View);
  187.  
  188. Menu = (function(_super) {
  189.  
  190.   __extends(Menu, _super);
  191.  
  192.   function Menu() {
  193.     this.bar = __bind(this.bar, this);
  194.     Menu.__super__.constructor.apply(this, arguments);
  195.   }
  196.  
  197.   Menu.prototype.tagName = 'ul';
  198.  
  199.   Menu.prototype.config = {
  200.     'bar': 'testing',
  201.     'class': 'menu',
  202.     'visible': false
  203.   };
  204.  
  205.   Menu.prototype.initialize = function() {
  206.     return Menu.__super__.initialize.call(this);
  207.   };
  208.  
  209.   Menu.prototype.bar = function(string) {
  210.     console.log(string);
  211.     return this;
  212.   };
  213.  
  214.   return Menu;
  215.  
  216. })(Component);
  217.  
  218. menu = new Menu({
  219.   'class': 'test-menu',
  220.   'css': {
  221.     'width': '300px',
  222.     'background-color': 'blue'
  223.   }
  224. })