Documentation
SheepJax is a library for ASP.NET MVC3 and jQuery to simplify the cumbersome code involved in writing Ajax application, as well as Reverse-Ajax (Comet).
Full Documentation
- Simple Ajax Call
- Reverse-Ajax/Comet
- (More articles to come...)
- Default Commands
- Custom JSON Converters
- Comet on a Web-Farm/Azure (SqlCommandHub)
- Custom Comet Hubs (e.g. MSMQ, AppFabric), using Reactive Extensions
Quick Syntax Overview
* Simple Ajax
JavaScript
$.sheepJax({
url: "@Url.Action("AddToCart")",
data: $("#add_to_cart_form").serialize(),
commands: {
FunctionOne: function(whatever, anything){
/* blah */
},
FunctionTwo: function(html){
/* blah */
},
FunctionThree: function(a, b, c){
/* blah */
}
}
});
MVC Controller (Strong-Type)
public ActionResult MyAction()
{
return SheepJax.On<IMyCommands>(cmd=>
{
// Invoking FunctionA JavaScript function
cmd.FunctionA(100, "Foo");
// Invoking FunctionB JavaScript function, while passing a ViewResult (will be transformed automatically to HTML)
cmd.FunctionB(ViewPartial("Bar"));
// Invoking FunctionC & FunctionA, using fluent method-chaining
cmd.FunctionC("Hello world", ViewPartial("Sheep"), 2)
.FunctionA(200, "Bar");
});
}
/// Strong-type proxy interface
public interface IMyCommands
{
void CommandOne(int whatever, string anything);
void CommandTwo(ViewResult html);
IMyCommands FunctionC(string a, ViewResult b, int c); // returns itself for chaining purpose
}
MVC Controller (Dynamic)
public ActionResult MyAction()
{
return SheepJax.Dynamic(cmd=>
{
// Invoking FunctionA JavaScript function
cmd.FunctionA(100, "Foo");
// Invoking FunctionB JavaScript function, while passing a ViewResult (will be transformed automatically to HTML)
cmd.FunctionB(ViewPartial("Bar"));
// Invoking FunctionC & FunctionA, using fluent method-chaining
cmd.FunctionC("Hello world", ViewPartial("Sheep"), 2)
.FunctionA(200, "Bar");
});
}
* Reverse-Ajax (Comet)
By default, it uses Long-Polling transport. HTML5 WebSockets support will come on ASP.NET4.5
Javascript(Same as that for simple Ajax)MVC Controller (Strong-Type)
public ActionResult MyAction()
{
return SheepJax.On<IMyCommands>().Comet(cmd=> // <- Note the Comet() extension method
{
// Invoking FunctionA JavaScript function immediately via Comet
cmd.FunctionA(100, "Foo");
Thread.Sleep(3000); // Pretend this is a slow operation
// Invoking FunctionB JavaScript function IMMEDIATELY via Comet, while passing a ViewResult (will be transformed automatically to HTML)
cmd.FunctionB(ViewPartial("Bar"));
Thread.Sleep(3000); // Pretend this is a slow operation
// Invoking FunctionC & FunctionA IMMEDIATELY via Comet, using fluent method-chaining
cmd.FunctionC("Hello world", ViewPartial("Sheep"), 2)
.FunctionA(200, "Bar");
});
}
MVC Controller (Dynamic)
public ActionResult MyAction()
{
return SheepJax.Dynamic().Comet(cmd=> // <- Note the Comet() extension method
{
// Invoking FunctionA JavaScript function immediately via Comet
cmd.FunctionA(100, "Foo");
Thread.Sleep(3000); // Pretend this is a slow operation
// Invoking FunctionB JavaScript function IMMEDIATELY via Comet, while passing a ViewResult (will be transformed automatically to HTML)
cmd.FunctionB(ViewPartial("Bar"));
Thread.Sleep(3000); // Pretend this is a slow operation
// Invoking FunctionC & FunctionA IMMEDIATELY via Comet, using fluent method-chaining
cmd.FunctionC("Hello world", ViewPartial("Sheep"), 2)
.FunctionA(200, "Bar");
});
}