Created
April 13, 2011 19:06
-
-
Save MarkBennett/918164 to your computer and use it in GitHub Desktop.
One way to write the BetaHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function countletters(event){ | |
| var max=jQuery(this).attr('maxlength'); | |
| var valLen=jQuery(this).val().length; | |
| if(valLen > max) { | |
| jQuery(this).val(jQuery(this).val().substring(0, max)); | |
| valLen=max; | |
| } | |
| jQuery("div.counter").text( valLen+'/'+max); | |
| } | |
| jQuery(function() { | |
| jQuery("div.countable textarea").bind('keyup', countletters); | |
| }); | |
| // This an Immediately Invoked Function Expression (IFEE) | |
| // | |
| // It serves two purposes: | |
| // | |
| // 1) it binds jQuery to $ so you can use $ safely | |
| // 2) it scopes all your variables so things don't accidently leak in the global (window) scope | |
| (function($) { | |
| // Your code goes inside here | |
| // A DataHub BetaFeature | |
| // | |
| // The attributes of this feature are stored in the DOM and this object | |
| // is associated with a root DOM element. | |
| // | |
| // This feature id is read from the first descendant element matching the ".feature-id" selector. | |
| // | |
| // This features description is read from the contents of the first descendent element matching the ".feature-desc" selector. | |
| // | |
| // The DOM should look something like this: | |
| // | |
| // <tr class="feature"> | |
| // <td><input class="feature-id" type="checkbox" value="1" name="beta_1005" id="beta_1005"></td> | |
| // <td><span class="feature-desc">Beta Feature 1005</span></td> | |
| // </tr> | |
| BetaFeature = function(elem) { | |
| this.$elem = $(elem); | |
| }; | |
| BetaFeature.prototype.getId = function() { | |
| var id; | |
| id = this.$elem.find('.feature-id input[type=checkbox]').attr("id").replace('beta_', ''); | |
| return id; | |
| }; | |
| BetaFeature.prototype.getDesc = function() { | |
| var desc; | |
| desc = this.$elem.find('.feature-desc').text(); // or this.$elem.find('.feature-desc').html() if you want the desc as HTML | |
| return desc; | |
| }; | |
| // Using a named function isn't neccesary but it makes debugging easier | |
| // since it shows up as a named function rather than an anonymous function | |
| // in the debugger. | |
| // | |
| // It also let's you re-use this function if you can generalize it in the future. | |
| function featureInputOnClick() { | |
| var $this, | |
| $feature_elem, | |
| feature, | |
| checked, | |
| dialogWidth; | |
| $this = $(this); | |
| $feature_elem = $this.closest('.feature'); | |
| feature = new BetaFeature($feature_elem); | |
| checked = ($(this).is(':checked')); | |
| if(checked){ | |
| alert('Hi There! The element was checked'); | |
| } else { | |
| dialogWidth = $(window).width() / 2; | |
| $("#text").text(feature.getDesc()); | |
| $("#dialog").dialog( {width: dialogWidth, buttons: { "OK": function() { $(this).dialog("close"); } }, modal: true } ); | |
| } | |
| } | |
| $(function() { | |
| $("span.check input[type=checkbox]").bind('click', featureInputOnClick); | |
| }); | |
| // Expose BetaFeature temporarily for debugging | |
| // This can be deleted later | |
| window.BetaFeature = BetaFeature; | |
| }(jQuery)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var feature = new BetaFeature(jQuery(".feature").first()); | |
| feature.getId(); | |
| feature.getDesc(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment