1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 sc_require('views/template');
  9 
 10 /** @class */
 11 
 12 SC.Checkbox = SC.TemplateView.extend(
 13   /** @scope SC.Checkbox.prototype */ {
 14 
 15   title: null,
 16   value: null,
 17 
 18   displayTitle: function() {
 19     var title = this.get('title');
 20     return title ? SC.String.loc(title) : null;
 21   }.property('title').cacheable(),
 22 
 23   classNames: ['sc-checkbox'],
 24   template: SC.Handlebars.compile('<label><input type="checkbox">{{displayTitle}}</label>'),
 25 
 26   didCreateLayer: function() {
 27     var self = this;
 28 
 29     this.$('input').bind('change', function() {
 30       self.domValueDidChange(this);
 31     });
 32   },
 33 
 34   domValueDidChange: function(node) {
 35     this.set('value', $(node).prop('checked'));
 36   },
 37 
 38   value: function(key, value) {
 39     if (value !== undefined) {
 40       this.$('input').prop('checked', value);
 41     } else {
 42       value = this.$('input').prop('checked');
 43     }
 44 
 45     return value;
 46   }.property()
 47 });
 48 
 49 SC.CheckboxSupport = /** @scope SC.CheckboxSupport */{
 50   didCreateLayer: function() {
 51     this.$('input').change(jQuery.proxy(function() {
 52       SC.RunLoop.begin();
 53       this.notifyPropertyChange('value');
 54       SC.RunLoop.end();
 55     }, this));
 56   },
 57 
 58   value: function(key, value) {
 59     if (value !== undefined) {
 60       this.$('input').prop('checked', value);
 61     } else {
 62       value = this.$('input').prop('checked');
 63     }
 64 
 65     return value;
 66   }.property().idempotent()
 67 };
 68 
 69