<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>true false maybe &#187; javascript</title>
	<atom:link href="http://truefalsemaybe.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://truefalsemaybe.com</link>
	<description>tom longson's blog on software, design, and user experience</description>
	<lastBuildDate>Sun, 07 Aug 2011 15:57:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>When form.submit() doesn&#8217;t refresh in IE6</title>
		<link>http://truefalsemaybe.com/2011/02/when-form-submit-doesnt-refresh-in-ie6/</link>
		<comments>http://truefalsemaybe.com/2011/02/when-form-submit-doesnt-refresh-in-ie6/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 00:34:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://truefalsemaybe.com/?p=181</guid>
		<description><![CDATA[I ran into a problem with some legacy code which looked like this: Delete And the corresponding function: function doDelete() { var submitForm = function() { document.forms[1].method="POST"; document.forms[1].action="myThing.do?dispatch=delete"; document.form[1].submit(); } var messageBoxConfig = { message:"Are you sure you want to delete?", title:"Confirm", style:YUI.widget.MessageBox.STYLES.CONFIRM, buttons:YUI.widget.MessageBox.BUTTONS.YESNO, callback:submitForm }; YUI.widget.MessageBox.show(messageBoxConfig); } Inline onclick handler aside (again legacy code), [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem with some legacy code which looked like this:</p>
<pre lang="html"><a title="Click on this link to Delete"
onclick="return doDelete()"
href="javascript:void(0);">Delete</a></pre>
<p>And the corresponding function:</p>
<pre lang="javascript">function doDelete() {
    var submitForm = function() {
        document.forms[1].method="POST";
	document.forms[1].action="myThing.do?dispatch=delete";
        document.form[1].submit();
    }
    var messageBoxConfig = {
		message:"Are you sure you want to delete?",
		title:"Confirm",
		style:YUI.widget.MessageBox.STYLES.CONFIRM,
		buttons:YUI.widget.MessageBox.BUTTONS.YESNO,
		callback:submitForm
    };
    YUI.widget.MessageBox.show(messageBoxConfig);
}</pre>
<p>Inline onclick handler aside (again legacy code), IE6 would submit the form without causing a page refresh. The form would submit transparently, leaving the user on the same page, which meant the backend wouldn&#8217;t have a chance to update the page with the results of the action. Firefox on the other hand, had no trouble submitting the form as I would expect.</p>
<p>In order to get IE6 to behave correctly, I had to use a setTimeout 0 in order to defer the action, and get IE6 to execute it.</p>
<pre lang="javascript">function doDelete() {
    var submitForm = function() {
        document.forms[1].method="POST";
	document.forms[1].action="myThing.do?dispatch=delete";
        setTimeout(function() {
            document.form[1].submit();
        },0);
    }
    var messageBoxConfig = {
		message:"Are you sure you want to delete?",
		title:"Confirm",
		style:YUI.widget.MessageBox.STYLES.CONFIRM,
		buttons:YUI.widget.MessageBox.BUTTONS.YESNO,
		callback:submitForm
    };
    YUI.widget.MessageBox.show(messageBoxConfig);
}</pre>
<p>From what I understand, Internet Explorer 6 has issues with submitting from an onclick event in an anchor, although when I remove the MessageBox code, it worked as expected again (without the setTimeout). Some people have reported onmousedown instead of onclick gets IE6 to work correctly. Possibly the default behavior of the anchor is causing it to break? Nevertheless, using timers to queue the submit action makes it work properly regardless of the browser. Will update this post if I find out more!</p>
<p>Related posts:</p>
<ul>
<li><a href="http://blog.stephenrushing.com/index.php/javascript/ie6-and-formsubmit-bug-workaround/">Stephen Rushing &#8220;IE6 formsubmit bug workaround&#8221;</a></li>
<li><a href="http://dev.tonic1394.com/2009/01/ie6-and-form-submit-fixery/">Tonic 1394&#8242;s IE6 and form submit fixery</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://truefalsemaybe.com/2011/02/when-form-submit-doesnt-refresh-in-ie6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PubSub in jQuery with Custom Events</title>
		<link>http://truefalsemaybe.com/2010/04/pubsub-in-jquery-with-custom-events/</link>
		<comments>http://truefalsemaybe.com/2010/04/pubsub-in-jquery-with-custom-events/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 21:08:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://truefalsemaybe.com/?p=135</guid>
		<description><![CDATA[Demo: PubSub in jQuery Recently I did a post on Data Binding in JavaScript using the YUI3 Library as my base. h3 and MikeInVB wanted to see it in jQuery, so I rewrote it and it turns out is a great example of PubSub (publish / subscribe) for jQuery. For this example, we&#8217;re using an [...]]]></description>
			<content:encoded><![CDATA[<p>Demo: <a href="http://jsbin.com/pubsub-jquery/">PubSub in jQuery</a></p>
<p>Recently I did a post on Data Binding in JavaScript using the YUI3 Library as my base. h3 and MikeInVB wanted to see it in jQuery, so I rewrote it and it turns out is a great example of PubSub (publish / subscribe) for jQuery. </p>
<p>For this example, we&#8217;re using an object named DB, which is short for DataBind. Basically it&#8217;s an object with getters and setters.</p>
<pre lang="javascript">
  var DB = function (data) {
      this._d = null;

      this.set = function (data) {
           this._d = data;
      };

      this.get = function () {
          return this._d;
      };

      this.set(data);
  };
</pre>
<p>Really, this is just an abstracted way to store data. To create an instance of it, set it and get the value back, we would do something like:</p>
<pre lang="javascript">
var myData = new DB();
myData.set("We can't fight in here, this is a war room!");
console.log(myData.get();) // "We can't fight in here, this is a war room!"
</pre>
<p>But lets say, we want to subscribe to this data object, and be notified whenever it changes. Instead of polling it and checking to see if the value is the same as last time, we can use jQuery&#8217;s custom events to trigger a custom event.</p>
<pre lang="javascript">
  var DB = function (data) {
      this._d = null;

      this.set = function (data) {
           this._d = data;
           // notify all subscribers when this function is used
           $(this).trigger("change");
      };

      this.get = function () {
          return this._d;
      };

      this.set(data);
  };
</pre>
<p>Now, we can subscribe to it like this:</p>
<pre lang="javascript">
$(myData).bind("change", function() {
     alert("The data was changed!");
});
</pre>
<p>Additionally, we can access that data in our handler: </p>
<pre lang="javascript">
$(myData).bind("change", function() {
     alert("My data was set to " + myData.get());
});
</pre>
<p>You can set up any number of subscribers to myData this way now, and they&#8217;ll be triggered when that instance is set(). </p>
<p>If you want to further improve DB, you can add a function to make it easier to attach change events, jQuery style:</p>
<pre lang="javascript">
  var DB = function (data) {
      this._d = null;

      this.set = function (data) {
           this._d = data;
           // notify all subscribers when this function is used
           $(this).trigger("change");
      };

      this.get = function () {
          return this._d;
      };

      // shortcut for functions trying to bind to the change custom event
      this.change = function (fn) {
          $(this).bind("change", fn);
      };

      this.set(data);
  };
</pre>
<p>With our new shortcut, we can subscribe to it like this:</p>
<pre lang="javascript">
var alexSez = new DB();
alexSez.set("Initiative comes to thems that wait.");
alexSez.change(function() {
    alert("Alex says: '" + myData.get() + "'");
});
alexSex.set("Welly, welly, welly, welly, welly, welly, well. To what do I owe the extreme pleasure of this surprising visit?")
// an alert will come up that says "Alex says: 'Welly, welly, welly, welly, welly, welly, well. To what do I owe the extreme pleasure of this surprising visit?'".
</pre>
<p>In the same way, you can set up two input boxes to &#8220;share&#8221; the same value by subscribing to a shared instance of DB like this:</p>
<pre lang="html">
<input type="text" id="in1"/>
<br/>
<input type="text" id="in2"/>
</pre>
<pre lang="javascript">
  var myData = new DB();

  var inputs = $("#in1, #in2");
  myData.change(function() {
      inputs.val(myData.get());
  });

  inputs.bind("keyup", function(ev) {
      myData.set($(ev.target).val());
  });
</pre>
<p>Ta da! </p>
<p>If you&#8217;re still interested in PubSub for jQuery, I recommend checking out <a href="http://jamiethompson.co.uk/web/2008/06/17/publish-subscribe-with-jquery/">Jamie Thompson&#8217;s</a> example using the $(document) namespace instead of specific object instances, or <a href="http://higginsforpresident.net/js/static/jq.pubsub.js">Peter Higgin&#8217;s jQuery.pubsub</a>, which <a href="http://weblog.bocoup.com/publishsubscribe-with-jquery-custom-events">reportedly</a> is significantly faster, although doesn&#8217;t allow object specific subscribing.</p>
<p>Demo: <a href="http://jsbin.com/pubsub-jquery/">PubSub in jQuery</a></p>
<p>What do you think? Would you do this differently? Leave a comment, I read them all!</p>
]]></content:encoded>
			<wfw:commentRss>http://truefalsemaybe.com/2010/04/pubsub-in-jquery-with-custom-events/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Data Binding in JavaScript</title>
		<link>http://truefalsemaybe.com/2010/04/data-binding-in-javascript/</link>
		<comments>http://truefalsemaybe.com/2010/04/data-binding-in-javascript/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 00:32:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://truefalsemaybe.com/?p=128</guid>
		<description><![CDATA[See the demo here: Data Binding in JavaScript Back when I was in Flex land one of the things that I found to be really neat was data binding&#8230; you could essentially have widgets that would update if the data updated automagically. I also wanted to take a stab at doing publishing and subscribing in [...]]]></description>
			<content:encoded><![CDATA[<p>See the demo here: <a href="http://jsbin.com/utaku/6/">Data Binding in JavaScript</a></p>
<p>Back when I was in Flex land one of the things that I found to be really neat was data binding&#8230; you could essentially have widgets that would update if the data updated automagically. I also wanted to take a stab at doing publishing and subscribing in YUI3, so built this an object called DB, which stores data. It can do three things, it can set() data, it can get() data, and it&#8217;s an EventTarget, so you can subscribe to it&#8217;s change event:</p>
<pre lang="javascript">
var DB = function (data) {
    this._d = null;

    this.publish("change",{emitFacade: true});

     this.set = function (data) {
         this._d = data;
         this.fire("change");
      };

      this.get = function () {
          return this._d;
      };

      this.set(data);
};
 Y.augment(DB, Y.EventTarget);
</pre>
<p>And to instantiate an instance of DB you just do <code>var myData = new DB();</code>, and then you can subscribe to it any number of times to be notified when the data has been updated. For example:</p>
<pre lang="html">
<input type="text" id="in1"/>
<br/>
<input type="text" id="in2"/>
</pre>
<pre lang="javascript">
var myData = new DB();
var inputs = Y.all("#in1, #in2");
myData.on("change", function() {
    inputs.each(function(node) {
        node.set('value', myData.get());
    });
});

inputs.on("keyup", function(ev) {
    myData.set(ev.currentTarget.get('value'));
});
</pre>
<p>This code is pretty simple. In YUI3 it creates an instance of DB, which has it&#8217;s own private data (unset initially), subscribes to the change event with a function that updates both text inputs on the page if myData gets set(), and then binds to the keyup event on the text inputs to call myData.set() whenever the contents of either text input has something typed into it. This results in a page where both inputs update whenever you type into either one. </p>
<p>See the demo here: <a href="http://jsbin.com/utaku/6/">Data Binding in JavaScript</a></p>
<p>Is this useful? Practical? UnJavaScripty? I don&#8217;t know, but I&#8217;d love to hear what you think <img src='http://truefalsemaybe.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://truefalsemaybe.com/2010/04/data-binding-in-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Better JavaScript Templating</title>
		<link>http://truefalsemaybe.com/2010/04/121/</link>
		<comments>http://truefalsemaybe.com/2010/04/121/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 23:51:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://truefalsemaybe.com/?p=121</guid>
		<description><![CDATA[I recently had to do some really heavy lifting with templates in JavaScript, and opted to use John Resig&#8217;s &#8220;Micro-templating&#8221; within the Underscore.js Library. It&#8217;s nice, you can write JavaScript within your templates to control your flow, like: It's been milliseconds since the epoch. Which, if you&#8217;re like me is great. You could use Google [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to do some really heavy lifting with templates in JavaScript, and opted to use John Resig&#8217;s &#8220;<a href="http://ejohn.org/blog/javascript-micro-templating/">Micro-templating</a>&#8221; within the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a> Library. It&#8217;s nice, you can write JavaScript within your templates to control your flow, like:</p>
<pre lang="html">
It's been <%= (new Date()).getTime() %> milliseconds since the epoch.
</pre>
<p>Which, if you&#8217;re like me is great. You could use <a href="http://code.google.com/closure/templates/">Google Closure Templates</a>, which probably is faster, but then you  would have to precompile your templates, which is a pain. </p>
<p>The only problem I personally had with the Resig solution was that if you tried to call a variable that didn&#8217;t exist within your data object, it would fail horrifically, so I modified it slightly to be slightly more forgiving:</p>
<pre lang="javascript">
  // JavaScript templating a-la ERB, pilfered from John Resig's
  // "Secrets of the JavaScript Ninja", page 83.
  // Single-quote fix from Rick Strahl's version.
  _.template = function(str, data) {
    var fn = new Function('obj',
      'var p=[],print=function(){p.push.apply(p,arguments);};' +
      'with(obj){p.push(\'' +
      str.replace(/[\r\t\n]/g, " ")
         .replace(/'(?=[^%]*%&gt;)/g,"\t")
         .split("'").join("\\'")
         .split("\t").join("'")
         .replace(//g, "',(typeof($1)!='undefined') ? $1:'','") // modified to allow undefined variables pass through
         .split("").join("p.push('")
         + "');}return p.join('');");
    return data ? fn(data) : fn;
  };
</pre>
<p>The 12th line where it says .replace(//g&#8230;) includes a test for the data that is being passed into the function that&#8217;s being created to output just an empty string if the variable is undefined. </p>
]]></content:encoded>
			<wfw:commentRss>http://truefalsemaybe.com/2010/04/121/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

