Sunday, February 20, 2011

jQuery plugin executes a callback based on a condition

    There is a Jquery utility that provides a way to execute callback functions based on one or more objects named jQuery.when(). However, when you need to do the same thing but based on one or more "conditions", how can you archive this?
    For example, you have a javascript interger. What you want to do is watching it's value and wait until the value matchs the expected value then you will execute a function. The value of this variable can be changed in various ways depend on users's activities.
    I know that we can simply work around this by checking that variable in the callback functions of jquery events like button clicked, textbox changed. But let's imagine you have alot of event that can change the value of that interger, in this case I don't wish to do that way. I did research on the Internet to find a proper solution but unfortunately, I had to make a solution myself. Personally, It's like a little cheat but I couldn't find any better way so I'm very grateful if someone suggests a better method.
Here is the supper simple implementation:
$.waitUntil = function (delegate, action, timeOutObj) {
    function _waitUntil(params) {
        $.waitUntil(params.condition, params.action, params.timeOutObj);
    }

    if (delegate.apply() == true) {
        action.apply();
    }
    else {
        if (timeOutObj != null) {
            clearTimeout(timeOutObj);
        }
        var param = { condition: delegate, action: action };
        param.timeOutObj = setTimeout(_waitUntil, 100, param);
    }
};

And here is the code sample:
var watchedItem = 0;
$.waitUntil(
    function() {
        return watchedItem > 2;
    },
    function() {
        alert('You have clicked 3 times');
    },
    null
);

Demo: Click to see the demo

Thursday, February 17, 2011

MVC Test Controller Builder and NSubstitute

If you are a web developer using ASP.NET MVC and Unit Test by NSubstitute, this blog is what you are looking for. I am sure there is at least one time you have problem writing unit test for controller but need to mock HttpContext. Whoever used MVCContrib would see that there is a class called TestControllerBuilder to help testing controller easily. However, this helper class is a part of RhinoMock or Moq to mock test objects. So if you want to use NSubstitute to mock objects, you cannot setup HttpContext. Actually you can use RhinoMock or Moq with NSubstitute but I think nobody does that. Using 2 mock frameworks in 1 test method is a little odd. To create mock object for HttpContext with NSubstitute, I decide to implement this helper class based on TestControllerBuilder of MvcContrib. Beside NSubstitute, this helper has a few APIs to mock HtmlHelper if you want to test HtmlHelper extension methods.
Below is the code sample:
[TestMethod]
public void Can_Export_Event_To_Add_To_Yahoo_Calendars()
{
  // Arrange
  EventController _controller = new EventController
  {
      ServiceFactory = Substitute.For<IServiceFactory>()
  };

  var builder = MvcTestControllerBuilder.GetDefaultBuilder();
  builder.InitializeController(_controller);
  builder.HttpContext.Request.Url.Returns(new Uri("http://domain.com/Event/Export"));

  Event evt = SetupEventData();

  // Act
  var result = _controller.Export(Guid.NewGuid(), evt.StartDate, "Yahoo");

  // Assert
  Assert.IsTrue(result is RedirectResult);
}
Source code: Download under WPFPL Updated: Fix the download link

Saturday, February 12, 2011

Looking for .NET Developers at Community Engine on the Northern Beaches in Sydney

I haven’t blogged for a while as I’ve been really busy in my new role at Community Engine where we are currently looking to expand our team. We’re doing some awesome stuff here to create a cutting edge product platform that will host our future client websites. The team is probably the best technically that I’ve worked with, and our implementation of SCRUM is the best I’ve seen. We’re also using all the latest and greatest technologies from Microsoft so it’s a great opportunity to get these skills and get them on your CV! The product platform is a multi-tenant web application hosted in the cloud with Amazon EC2 written with ASP.NET MVC and .NET 4 and Entity Framework 4. We’re also making use of MongoDB which is used by the likes of FourSquare and SourceForge, along with Solr for searching and distributed caching with Redis. Who wouldn’t want to work with this technology stack? If you want to get involved you can apply through our careers page. Make sure you mention my blog when you do. We are also interested in hearing from candidates overseas. We are looking for a Technical Lead and a number of mid/senior developers, as well as many other roles: - Digital BDM - Front end / UX developers - Junior Project Manager - Digital Project Manager - Customer Support Officer - Web Business Analyst - System Administrator - Tech Writer - Test Analyst Cheer