Saturday, June 06, 2009
So who is this old browser anyway?
Posted by
William
at
12:48 PM
permalink
0
comments
Labels: internet explorer, just for fun, microsoft
Monday, May 18, 2009
Extension Method in C# Rocks
While I always say ASP.NET SXXKS, at the same time I always give credits to the great C# language itself, its very robust, high performance, and the best of all is: the team learn a lot of great syntax from other languages.
I have been writing Ruby for almost 2 years, one of the coolest thing you found in Ruby (and many dynamic languages) are the ability to extend your class dynamically with your own methods (in Ruby you use module include/extend approach). Now C# have this very simple yet powerful addition known as Extension Method.
I want to show you a 2 scenarios I have come across recently and how to make the syntax looks less obstructive with Extension Method:
1. String comparison with default values
You might have a situation where a null string value, or a default value, should always taken as empty (common situations like your form has a default value filled in, but user could deletes that and submit an empty string). In the past (since MS Access) there is a method called NZ, or NullZero, which does some kind of value if null check. The method is a utility method which you have to invoke that as a function. In C# I used to write a static class called StringUtil and do whatever I need to do there with a function calling approach. Not any longer with extension method.
Notice the magical this keyword:
this string value
the compiler would automagically (yeah, works like a magic) bind this method into the type you are specifying. Now your method is available for every single string in your project once you have used the namespace containing this extension. Nice.
2. Lots more formatting methods with a extension method scope
So you have seen the long list of date formats available in DateTime, but for some reason it's always not the one you want (e.g. you host 20+ projects on a single server with different locale requirement, obviously most of the ToXXXString method are going to look like trash for you as they all act upon default locale). Instead of dumping all extension method into DateTime, I decided to scope them inside a dynamically created formatter class:
The real extension method only creates the formatter class, then everything I really want to make available are created inside the formatter class methods. If you combine this further with a dynamic factory approach to create your own custom formatter on the fly.... man this is going to be so cool :)
Posted by
William
at
11:58 AM
permalink
0
comments
Friday, May 08, 2009
It turns out livequery is still very important on jQuery 1.3
When jQuery introduced live and die event, we thought we don't need livequery plugin for hooking AJAX update element events.
True for the statement above, but if you think you don't need livequery anymore, not quite.
The truth is you still have something other than AJAX that requires livequery. This plugin has a very interesting feature which allows you to hook a function base on a new element addition into the document which matches your defined selector. With so many hard to deal with layout issues still on modern browser (the recent one I face is variable height div block needs vertical alignment), this is very handy as it make sure regardless your element comes in at the beginning or as AJAX update later, the hack is always applied to corresponding elements.
I only wish the entire livequery integrate with jQuery, they are all so useful that I couldn't think of a case to go without now.
Posted by
William
at
1:49 PM
permalink
0
comments
Labels: javascript, jquery, livequery, plugin
Wednesday, May 06, 2009
So you want a javascript html template... PURE.
Stefan sent me this link:
http://beebole.com/pure/
This thing rocks. I think everyone (especially those who have used JSON) should have come across need to do HTML template on the client JS and update the content when actual JSON data comes back. This could be really handy, and of coz, like many great libraries, its jQuery based.
Posted by
William
at
2:52 PM
permalink
2
comments
Labels: javascript, jquery
Tuesday, April 28, 2009
The ASP.NET WebForm way of AJAX...
I have been working for an ASP.NET AJAX project recently, to do a partial update, instead of issuing $.load/$.post in jquery, I have to:
1. Create a display:none div with a hidden field and a button
2. Add the button to async post using ScriptManager.RegisterAsyncPostControl
3. When user click the link collection I have generated runtime in client side (so thats why I can't hook the event before hand):
a. Add an pageLoaded handler for displaying the ajax dialog
b. Feed the form hidden value
c. Trigger the click event of the button
d. remove the pageLoaded handler after use
Yuck... thats very long. Any better way?
If I am using ASP.NET MVC (or Ruby, php...) I just need:
0. Render those hyperlink normally (thats not a step...)
1. Bind live event on click for all anchor:
a. $.post
b. use the return function to do a $().html() update
That makes ASP.NET WebForm so awful to use...
Friday, April 24, 2009
HKROR April Meetup
Ok we have finished our April meetup :) For those who haven't come, here is a brief summary:
1. I am the speaker for jQuery + Rails, I have talk about issues you would encounter when trying to use jQuery with Rails, workaround, and then a tic-tac-toe demo with json and js eval design.
2. Aaron Farr did a demo for Phusion Passenger + Apache, some hiccups but finally things works :)
For my presentation, here is my slides and demo code. Feel free to grab it (and hope it doesn't break my bandwidth limit :P)
Update: Its not an obligation, but I would be very interested to know who has interest on my demo, so it would be great if you could just leave me a simple comment :)
Posted by
William
at
4:32 PM
permalink
0
comments
Labels: hkror, ruby on rails
Saturday, April 18, 2009
OK... mod_rails + nginx, dont get too excited just yet
I was very excited about the news until I really test it myself. As its a 1.0 support for nginx, as usual things broke:
- If your have PCRE installed at system instead of linking it with the nginx compilation, it would by accident proceed without linking it. i.e. All your rewrite rules will no longer work
- I tried to go without the rewrite and just put phusion up, performance is terrible. I really mean it- the images serving speed is a lot slower than my thin+nginx, which I think the primary reason could be mod_rails is trying to serve the binary file within ruby, which makes #1 very important.
- The demo nginx.conf does not show you how to mix passenger with nginx rewrite for static content- I think thats 99.99% ex-nginx user expects to do it, so probably need a better user guide
- People are also saying the default nginx compile does not come with SSL. That would be another piece of important things to miss
Posted by
William
at
11:10 AM
permalink
0
comments
Labels: mod_rails, nginx, ruby on rails


