<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Sayed Ibrahim Hashimi - MSBuild, Web Deploy (MSDeploy), ASP.NET - jQuery</title>
    <link>http://sedodream.com/</link>
    <description>MSBuild, C#, Visual Studio and more</description>
    <language>en-us</language>
    <copyright>Sayed Ibrahim Hashimi</copyright>
    <lastBuildDate>Fri, 02 Apr 2010 05:24:30 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>sayed.hashimi@gmail.com</managingEditor>
    <webMaster>sayed.hashimi@gmail.com</webMaster>
    <item>
      <trackback:ping>http://sedodream.com/Trackback.aspx?guid=beb40727-c878-440d-bb2d-80501bb77312</trackback:ping>
      <pingback:server>http://sedodream.com/pingback.aspx</pingback:server>
      <pingback:target>http://sedodream.com/PermaLink,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</pingback:target>
      <dc:creator>Ibrahim</dc:creator>
      <wfw:comment>http://sedodream.com/CommentView,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</wfw:comment>
      <wfw:commentRss>http://sedodream.com/SyndicationService.asmx/GetEntryCommentsRss?guid=beb40727-c878-440d-bb2d-80501bb77312</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The story around <a href="http://msdn.microsoft.com/en-us/library/cc668201%28VS.100%29.aspx">routing</a> in
ASP.NET MVC is pretty good. When a resource is requested the action that it is routed
to is determined and the parameters to the action are initialized. Counter to that
when you build a link you specify the argument values and it creates the url for you.
So if your routes happen to change your application shouldn’t be bothered by that.
One area which can be problematic for this is if you are making any ajax requests.
What I’ve done to minimize the impact of route changes to Ajax requests is to have
the script make a call to determine the correct route. In the most trivial case this
is extremely easy. You call an action, which has a known route, passing into it the
name of the action and controller. It returns to you the url for the route. This works
really good, but the problem is when your routes have parameters, how do you handle
those?
</p>
        <p>
First let’s take a look at the Javascript and then the definition of the action method
which it calls.
</p>
        <pre class="brush: js;">// wire up the event for the enter button
$("#searchText").keypress(function (event) {
    if (event.keyCode == 13) {
        // grab the text that is inside the box
        var text = $("#searchText").val();

        var routData = {
            controllerName: 'Search',
            actionName: 'Search',
            paramValues: "{ criteria: '" + text + "' }"
        };

        $.ajax({
            url: "/Home/MapRoute",
            data: routData,
            success: function (data) {
                window.location.href = data;
            },
            error: function (data) {
                alert('there was a failure on the internets');
            }
        });

    }
});</pre>
        <p>
Here I am building a Javascript object named routeData. This is declared in <a href="http://json.org/">JSON</a>.
One thing to pay attention to is the fact that the value for <em><strong>paramValues</strong></em> is
a string containing JSON. This is passed to the controller action.
</p>
        <p>
I’m using <a href="http://jquery.com/">jQuery</a> to make an Ajax request to <em>/Home/MapRoute</em>.
This maps to the method shown below.
</p>
        <pre class="brush: csharp;">public JsonResult MapRoute(string actionName, string controllerName, string paramValues)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();

    Dictionary&lt;string, string&gt; parameters = jss.Deserialize&lt;Dictionary&lt;string, string&gt;&gt;(paramValues);

    RouteValueDictionary rd = new RouteValueDictionary();
    foreach (string key in parameters.Keys)
    {
        rd.Add(key, parameters[key]);
    }

    UrlHelper urlHelper = new UrlHelper(this.Request.RequestContext);
    string url = urlHelper.Action(actionName, controllerName, rd);

    return Json(url, JsonRequestBehavior.AllowGet);
}</pre>
        <p>
Here I’m using the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx">JavaScriptSerializer</a> to
convert the JSON string into a dictionary, with string as key and value. I use that
dictionary to create a <a href="http://msdn.microsoft.com/en-us/library/system.web.routing.routevaluedictionary.aspx">RouteValueDictionary</a> which
is passed, along with other parameters, into the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx">UrlHelper</a> to
generate the url. When you return the Json result you must specify <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28VS.100%29.aspx">JsonRequestBehavior</a>.AllowGet,
otherwise a <strong><em>500 internal service error</em></strong> will be returned.
I think this is new with ASP.NET 2.
</p>
        <p>
When the action method returns, you can use that url. The drawback to this approach
is that you will make an extra request to determine the url, but you will be sure
that those urls are correct. Also you could cache the results with <a href="http://www.asp.net/learn/mvc/tutorial-15-cs.aspx">Output
Caching</a> since the routes won’t change.
</p>
        <p>
Sayed Ibrahim Hashimi
</p>
      </body>
      <title>ASP.NET MVC Route + Ajax + jQuery</title>
      <guid isPermaLink="false">http://sedodream.com/PermaLink,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</guid>
      <link>http://sedodream.com/2010/04/02/ASPNETMVCRouteAjaxJQuery.aspx</link>
      <pubDate>Fri, 02 Apr 2010 05:24:30 GMT</pubDate>
      <description>&lt;p&gt;
The story around &lt;a href="http://msdn.microsoft.com/en-us/library/cc668201%28VS.100%29.aspx"&gt;routing&lt;/a&gt; in
ASP.NET MVC is pretty good. When a resource is requested the action that it is routed
to is determined and the parameters to the action are initialized. Counter to that
when you build a link you specify the argument values and it creates the url for you.
So if your routes happen to change your application shouldn’t be bothered by that.
One area which can be problematic for this is if you are making any ajax requests.
What I’ve done to minimize the impact of route changes to Ajax requests is to have
the script make a call to determine the correct route. In the most trivial case this
is extremely easy. You call an action, which has a known route, passing into it the
name of the action and controller. It returns to you the url for the route. This works
really good, but the problem is when your routes have parameters, how do you handle
those?
&lt;/p&gt;
&lt;p&gt;
First let’s take a look at the Javascript and then the definition of the action method
which it calls.
&lt;/p&gt;
&lt;pre class="brush: js;"&gt;// wire up the event for the enter button
$(&amp;quot;#searchText&amp;quot;).keypress(function (event) {
    if (event.keyCode == 13) {
        // grab the text that is inside the box
        var text = $(&amp;quot;#searchText&amp;quot;).val();

        var routData = {
            controllerName: 'Search',
            actionName: 'Search',
            paramValues: &amp;quot;{ criteria: '&amp;quot; + text + &amp;quot;' }&amp;quot;
        };

        $.ajax({
            url: &amp;quot;/Home/MapRoute&amp;quot;,
            data: routData,
            success: function (data) {
                window.location.href = data;
            },
            error: function (data) {
                alert('there was a failure on the internets');
            }
        });

    }
});&lt;/pre&gt;
&lt;p&gt;
Here I am building a Javascript object named routeData. This is declared in &lt;a href="http://json.org/"&gt;JSON&lt;/a&gt;.
One thing to pay attention to is the fact that the value for &lt;em&gt;&lt;strong&gt;paramValues&lt;/strong&gt;&lt;/em&gt; is
a string containing JSON. This is passed to the controller action.
&lt;/p&gt;
&lt;p&gt;
I’m using &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; to make an Ajax request to &lt;em&gt;/Home/MapRoute&lt;/em&gt;.
This maps to the method shown below.
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;public JsonResult MapRoute(string actionName, string controllerName, string paramValues)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();

    Dictionary&amp;lt;string, string&amp;gt; parameters = jss.Deserialize&amp;lt;Dictionary&amp;lt;string, string&amp;gt;&amp;gt;(paramValues);

    RouteValueDictionary rd = new RouteValueDictionary();
    foreach (string key in parameters.Keys)
    {
        rd.Add(key, parameters[key]);
    }

    UrlHelper urlHelper = new UrlHelper(this.Request.RequestContext);
    string url = urlHelper.Action(actionName, controllerName, rd);

    return Json(url, JsonRequestBehavior.AllowGet);
}&lt;/pre&gt;
&lt;p&gt;
Here I’m using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx"&gt;JavaScriptSerializer&lt;/a&gt; to
convert the JSON string into a dictionary, with string as key and value. I use that
dictionary to create a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.routing.routevaluedictionary.aspx"&gt;RouteValueDictionary&lt;/a&gt; which
is passed, along with other parameters, into the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx"&gt;UrlHelper&lt;/a&gt; to
generate the url. When you return the Json result you must specify &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28VS.100%29.aspx"&gt;JsonRequestBehavior&lt;/a&gt;.AllowGet,
otherwise a &lt;strong&gt;&lt;em&gt;500 internal service error&lt;/em&gt;&lt;/strong&gt; will be returned.
I think this is new with ASP.NET 2.
&lt;/p&gt;
&lt;p&gt;
When the action method returns, you can use that url. The drawback to this approach
is that you will make an extra request to determine the url, but you will be sure
that those urls are correct. Also you could cache the results with &lt;a href="http://www.asp.net/learn/mvc/tutorial-15-cs.aspx"&gt;Output
Caching&lt;/a&gt; since the routes won’t change.
&lt;/p&gt;
&lt;p&gt;
Sayed Ibrahim Hashimi
&lt;/p&gt;</description>
      <comments>http://sedodream.com/CommentView,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Javascript</category>
      <category>jQuery</category>
      <category>routing</category>
    </item>
    <item>
      <trackback:ping>http://sedodream.com/Trackback.aspx?guid=539af450-2b61-46e1-a6de-8a62cbb5b4f9</trackback:ping>
      <pingback:server>http://sedodream.com/pingback.aspx</pingback:server>
      <pingback:target>http://sedodream.com/PermaLink,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</pingback:target>
      <dc:creator>Ibrahim</dc:creator>
      <wfw:comment>http://sedodream.com/CommentView,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</wfw:comment>
      <wfw:commentRss>http://sedodream.com/SyndicationService.asmx/GetEntryCommentsRss?guid=539af450-2b61-46e1-a6de-8a62cbb5b4f9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Have you seen those <a href="http://sedotech.com/LiveDemo/TextWithHints">text
boxes</a> on the web that have a hint contained inside of it and wondered how you
could implement that? It's pretty easy with <a href="http://jquery.com/">jQuery</a>,
and there already exist some plugins that you can use, for example here is <a href="http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/">one</a>.
When I set out to do this I didn’t even look to see what was out there because I wanted
to write a <a href="http://plugins.jquery.com/">jQuery plugin</a>, but the solution
that I can up with is not that different from that one. 
<p>
Here is how I wanted the plugin to behave
</p><ol><li>
Add a specified hint to the text box, if the input element was not focused and empty 
</li><li>
When the text box was focused, the hint should disappear 
</li><li>
When a form is submitted all hints should be removed prior to ensure that they are
not incorrectly submitted 
</li></ol><p>
First what I did was to create the a file named <em>jquery.sedotech.inputWithHint.js</em>.
You should name your plugins using this naming convention
</p><p><em>jquery.<strong>customString</strong>.<strong>pluginName</strong>.js</em></p><p>
Here is the source for the plugin
</p><pre class="brush: js;">(function ($) {
    var hintClassName = 'inputWithTextHint';

    $.fn.addHint = function (hint) {
        var filteredSet = this.filter('input:text, textarea');
        filteredSet.each(function () {
            // In here 'this' refers to an individual element
            doAddHint($(this), hint);
        });

        // Find all forms and update the pre post to remove the hint
        $('form input:submit').click(function () {
            $('input:text, textarea').each(function () {
                if ($(this).hasClass(hintClassName) &amp;&amp; $(this).attr('hint')
                &amp;&amp; $(this).val() == $(this).attr('hint')) {
                    $(this).val('');
                }
            });
        });
    }

    function doAddHint(target, hint) {
        // Only add hint if the target is empty
        if (target.val() == '') {
            addHintToInput(target, hint);

            target.focus(function () {
                // If the target has the hint class on it then a hint must be showing
                //  when hint is showing put cursor at the begining
                if ($(this).hasClass(hintClassName)) {
                    // remove the hint
                    $(this).val('');
                    // remove class
                    $(this).removeClass(hintClassName);
                }
            });

            target.blur(function () {
                // If no text then add hint class back
                if ($(this).val() == '') {
                    addHintToInput(target, hint);
                }
            });
        }
    }

    function addHintToInput(target, hint) {
        target.val(hint);
        target.addClass(hintClassName);
        // add attribute to the target to store hint
        target.attr('hint', hint);
    }
})(jQuery);</pre>
Some things to take note of. When you are creating a plugin in you should use the
pattern <pre class="brush: js;">(function ($) {
    // plugin code here
})(jQuery);</pre><p>
Take note of the <strong>($)</strong> as the parameter and <strong>(jQuery)</strong> at
the end. What is happening here is that you are defining an anonymous function declaring
a parameter named $ and then invoking that function passing in the <strong>jQuery</strong> object.
You do this because when you are authoring plugins the $ variable is not available,
you have to use the other alias jQuery, but that’s just way too difficult. If you
use the pattern you are ensured that the <strong>$</strong> alias is available and
it won’t conflict with other Javascript libraries.
</p><p>
Beyond this all I’m doing is filtering the list of items which are passed in, with
the expression <strong><em>var filteredSet = this.filter('input:text, textarea')</em></strong>,
to make sure that the plugins doesn’t touch elements which it is not familiar with
modifying. After I add the hint, by calling <strong><em>doAddHint</em></strong> on
each element in the filtered set, I make sure that the forms on the page are not submitted
with those hints.
</p><h3>Resource Links
</h3><ul><li><a href="http://sedotech.com/Resources#jQuery">All my jQuery resources</a></li><li><a href="http://sedotech.com/LiveDemo/TextWithHints">Live Demo for this plugin</a></li></ul><p>
Sayed Ibrahim Hashimi
</p></body>
      <title>jQuery: Creating a plugin to place hints in text boxes</title>
      <guid isPermaLink="false">http://sedodream.com/PermaLink,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</guid>
      <link>http://sedodream.com/2010/03/31/jQueryCreatingAPluginToPlaceHintsInTextBoxes.aspx</link>
      <pubDate>Wed, 31 Mar 2010 04:24:07 GMT</pubDate>
      <description>Have you seen those &lt;a href="http://sedotech.com/LiveDemo/TextWithHints"&gt;text boxes&lt;/a&gt; on
the web that have a hint contained inside of it and wondered how you could implement
that? It's pretty easy with &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;, and there already
exist some plugins that you can use, for example here is &lt;a href="http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/"&gt;one&lt;/a&gt;.
When I set out to do this I didn’t even look to see what was out there because I wanted
to write a &lt;a href="http://plugins.jquery.com/"&gt;jQuery plugin&lt;/a&gt;, but the solution
that I can up with is not that different from that one. 
&lt;p&gt;
Here is how I wanted the plugin to behave
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Add a specified hint to the text box, if the input element was not focused and empty 
&lt;/li&gt;
&lt;li&gt;
When the text box was focused, the hint should disappear 
&lt;/li&gt;
&lt;li&gt;
When a form is submitted all hints should be removed prior to ensure that they are
not incorrectly submitted 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
First what I did was to create the a file named &lt;em&gt;jquery.sedotech.inputWithHint.js&lt;/em&gt;.
You should name your plugins using this naming convention
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;jquery.&lt;strong&gt;customString&lt;/strong&gt;.&lt;strong&gt;pluginName&lt;/strong&gt;.js&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Here is the source for the plugin
&lt;/p&gt;
&lt;pre class="brush: js;"&gt;(function ($) {
    var hintClassName = 'inputWithTextHint';

    $.fn.addHint = function (hint) {
        var filteredSet = this.filter('input:text, textarea');
        filteredSet.each(function () {
            // In here 'this' refers to an individual element
            doAddHint($(this), hint);
        });

        // Find all forms and update the pre post to remove the hint
        $('form input:submit').click(function () {
            $('input:text, textarea').each(function () {
                if ($(this).hasClass(hintClassName) &amp;amp;&amp;amp; $(this).attr('hint')
                &amp;amp;&amp;amp; $(this).val() == $(this).attr('hint')) {
                    $(this).val('');
                }
            });
        });
    }

    function doAddHint(target, hint) {
        // Only add hint if the target is empty
        if (target.val() == '') {
            addHintToInput(target, hint);

            target.focus(function () {
                // If the target has the hint class on it then a hint must be showing
                //  when hint is showing put cursor at the begining
                if ($(this).hasClass(hintClassName)) {
                    // remove the hint
                    $(this).val('');
                    // remove class
                    $(this).removeClass(hintClassName);
                }
            });

            target.blur(function () {
                // If no text then add hint class back
                if ($(this).val() == '') {
                    addHintToInput(target, hint);
                }
            });
        }
    }

    function addHintToInput(target, hint) {
        target.val(hint);
        target.addClass(hintClassName);
        // add attribute to the target to store hint
        target.attr('hint', hint);
    }
})(jQuery);&lt;/pre&gt;
Some things to take note of. When you are creating a plugin in you should use the
pattern &lt;pre class="brush: js;"&gt;(function ($) {
    // plugin code here
})(jQuery);&lt;/pre&gt;
&lt;p&gt;
Take note of the &lt;strong&gt;($)&lt;/strong&gt; as the parameter and &lt;strong&gt;(jQuery)&lt;/strong&gt; at
the end. What is happening here is that you are defining an anonymous function declaring
a parameter named $ and then invoking that function passing in the &lt;strong&gt;jQuery&lt;/strong&gt; object.
You do this because when you are authoring plugins the $ variable is not available,
you have to use the other alias jQuery, but that’s just way too difficult. If you
use the pattern you are ensured that the &lt;strong&gt;$&lt;/strong&gt; alias is available and
it won’t conflict with other Javascript libraries.
&lt;/p&gt;
&lt;p&gt;
Beyond this all I’m doing is filtering the list of items which are passed in, with
the expression &lt;strong&gt;&lt;em&gt;var filteredSet = this.filter('input:text, textarea')&lt;/em&gt;&lt;/strong&gt;,
to make sure that the plugins doesn’t touch elements which it is not familiar with
modifying. After I add the hint, by calling &lt;strong&gt;&lt;em&gt;doAddHint&lt;/em&gt;&lt;/strong&gt; on
each element in the filtered set, I make sure that the forms on the page are not submitted
with those hints.
&lt;/p&gt;
&lt;h3&gt;Resource Links
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://sedotech.com/Resources#jQuery"&gt;All my jQuery resources&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://sedotech.com/LiveDemo/TextWithHints"&gt;Live Demo for this plugin&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Sayed Ibrahim Hashimi
&lt;/p&gt;</description>
      <comments>http://sedodream.com/CommentView,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</comments>
      <category>Javascript</category>
      <category>jQuery</category>
      <category>jQuery-plugin</category>
    </item>
  </channel>
</rss>