<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
  <channel>
    <title>ASP.NET AJAX: Half-way between createDelegate() and createCallback()</title>
    <link>http://blogs.lotterypost.com/speednet/2008/1/aspnet-ajax-half-way-between-createdelegate.htm</link>
    <atom:link href="http://www.lotterypost.com/rss/blogcomments/20647" rel="self" type="application/rss+xml" />
    <description>Speednet's Blog: ASP.NET AJAX: Half-way between createDelegate() and createCallback()</description>
    <dc:language>en-us</dc:language>
    <generator>Lottery Post RSS Generator</generator>
    <item>
      <title>Original Blog Entry: ASP.NET AJAX: Half-way between createDelegate() and createCallback()</title>
      <link>http://blogs.lotterypost.com/speednet/2008/1/aspnet-ajax-half-way-between-createdelegate.htm</link>
      <guid isPermaLink="true">http://blogs.lotterypost.com/speednet/2008/1/aspnet-ajax-half-way-between-createdelegate.htm</guid>
      <pubDate>Mon, 14 Jan 2008 19:47:12 GMT</pubDate>
      <dc:creator>Speednet</dc:creator>
      <description><![CDATA[<p>I stumbled on a use of <span style="font-family: courier new,courier">Function.createDelegate()</span> method today that seems to be a cross between <span style="font-family: courier new,courier">Function.createDelegate()</span> and <span style="font-family: courier new,courier">Function.createCallback()</span>.</p><p>I had the need to pass a context to a handler function, with the context being a JSON object containing the state of a few variables.&nbsp; I tried using <span style="font-family: courier new,courier">createCallback()</span>, which is designed for that purpose, but it did not seem to work well because I was not using it together with a DOM event &mdash; just with an internal function call.</p><p>So I turned to <span style="font-family: courier new,courier">createDelegate()</span>, using the JSON object as the <span style="font-family: courier new,courier">this</span> argument, and it worked quite nicely.</p><p>An example of this syntax is as follows.&nbsp; The code assigns a function to the variable fn that (when called) displays an alert box containing the element number and id of a DIV with an ID of &quot;ThisOne&quot;.&nbsp; There are certainly more useful uses for this method, but it should provide a clear example of how it works.&nbsp; The key point here is the <span style="font-family: courier new,courier">function()</span> assignment that uses <span style="font-family: courier new,courier">this</span> to utilize the values passed in the delegate.</p><p><span style="font-size: 11px; font-family: courier new,courier">var a = document.getElementsByTagName(&quot;div&quot;);<br />var fn;</span></p><span style="font-size: 11px; font-family: courier new,courier">for (var x=0; x &lt; a.length; x++) {<br />&nbsp; &nbsp; if (a[x].id == &quot;ThisOne&quot;) {<br />&nbsp; &nbsp; &nbsp; &nbsp; fn = Function.createDelegate({<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e: a[x],<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num: x<br />&nbsp; &nbsp; &nbsp; &nbsp; }, function () {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;DIV #&quot;+this.num+&quot;: &quot;+this.e.id);<br />&nbsp; &nbsp; &nbsp; &nbsp; });<br />&nbsp; &nbsp; }<br />}</span><p><span style="font-size: 11px; font-family: courier new,courier">fn();</span></p><p>If you're not sure about the value of the above code, ask yourself, &quot;How would I code the same thing without the <span style="font-family: courier new,courier">createDelegate()</span>?&quot;&nbsp; Also a....</p><p>[ <a href="http://blogs.lotterypost.com/speednet/2008/1/aspnet-ajax-half-way-between-createdelegate.htm">More</a> ]</p>]]></description>
      <category>* Original Blog Entry</category>
      <wfw:comment>http://www.lotterypost.com/blogentry/20647</wfw:comment>
    </item>
  </channel>
</rss>