Last active
January 3, 2016 15:39
-
-
Save justinbmeyer/8483924 to your computer and use it in GitHub Desktop.
Shows a custom `fade-in-when` attribute that fades in its element when a certain value in the scope is truthy.
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
| <div id="app"></div> | |
| <script src="../../lib/steal/steal.js"></script> | |
| <script type="text/mustache" id="info-template"> | |
| <input type='radio' can-value="toggling" value="moreInfoShowing"/> moreInfoShowing</br> | |
| <input type='radio' can-value="toggling" value="moreInfoVisible"/> moreInfoVisible</br> | |
| <button can-click="toggle">Toggle {{toggling}}</button> | |
| <div fade-in-when="{{toggling}}"> | |
| More Info | |
| </div> | |
| </script> | |
| <script> | |
| steal("can/view/mustache", "can/view/bindings",function(){ | |
| can.view.attr("fade-in-when", function( el, attrData ) { | |
| var fadeInCompute, | |
| // handler for when the compute changes | |
| handler = function(ev, newVal, oldVal){ | |
| if(newVal && !oldVal) { | |
| $(el).fadeIn("slow") | |
| } else if(!newVal){ | |
| $(el).hide() | |
| } | |
| } | |
| teardownComputeBinding = function(){ | |
| fadeInCompute && fadeInCompute.unbind(handler) | |
| }, | |
| setupComputeBinging = function(){ | |
| var attrValue = el.getAttribute("fade-in-when"); | |
| fadeInCompute = attrData.scope.computeData(attrValue).compute; | |
| console.log("Listening to ", attrValue, "in the scope") | |
| fadeInCompute.bind("change",handler); | |
| handler( {}, fadeInCompute() , undefined); | |
| } | |
| $(el).bind("attributes", function(ev){ | |
| if( ev.attributeName == "fade-in-when" ) { | |
| teardownComputeBinding(); | |
| // fadeInWhen might have been removed | |
| if( el.getAttribute("fade-in-when") ) { | |
| setupComputeBinging(); | |
| } | |
| } | |
| }).bind("remove", function(){ | |
| teardownComputeBinding && teardownComputeBinding(); | |
| }) | |
| setupComputeBinging() | |
| }) | |
| var toggles = { | |
| toggling: can.compute("moreInfoShowing"), | |
| moreInfoShowing: can.compute(true), | |
| moreInfoVisible: can.compute(false), | |
| toggle: function(){ | |
| var compute = toggles[toggles.toggling()]; | |
| compute(!compute()) | |
| } | |
| }; | |
| $("#app").html( can.view("info-template",toggles) ) | |
| }) | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment