JsMapper

JsMapper creates Javascript ViewModel-Code (support for Backbone and KnockoutJs atm) from your
(Domain-) Classes with support of DataAnnotations.


Why?

People who are writing backend-systems with ASP.NET Web API and/or SignalR have very cool possibilities to serve rich web-frontends.
Thanks to ViewModel-libraries like Backbone.JS, you can easily manipulate data-records with client-side validations, included GET/POST/PUT/DELETE-operations and much more.

JsMapper allows you to create these viewModels with almost no effort.
Currently you can choose between Backbone and KnockoutJS as output.

What's new in Version 0.2?

Version 0.2 comes with a Standalone-Exporter, as well as support for TypeScript.

Simple Usage-Exapmle

Here's an example of a simple class:
[JsModel("api/employee")]
public class Employee
{
    [Key]
    public int Id { get; set; }

    [Required, MaxLength(30)]
    public string Name { get; set; }

    [MinLength(2), DefaultValue("Mr.")]
    public string Salutation { get; set; }

    [EmailAddress]
    public string EmailAddress { get; set; }
}
As you can see, all you need to do is to add the JsModelAttribute to your class.
If you're already using ValidationAttributes, you'll get client-side ViewModel-validation for free :)

On your server-initialisation (for exapmle Global.asax.cs), you need to configurate the JsModelManager.
You can use the Fluent-Interface to do so:
JsModelManager.ConfigMngr.Configurate(
    new JsModelFluentConfig()
        .UseDefaultNamespace("app.models")
        .ScanForViewModels()
        .AndCacheOutput()
);

Next step is to get the output.
In this exapmle, I am using ASP.NET MVC and I create a JsController, which returns the MVVM as Backbone-Model:
public ActionResult Models()
{
    var m = JsModelManager.GetAllJsOutput<Backbone>();
    return JavaScript(m);
}

Done!
Just reference the javascript-libs (in this case backbone.js) and the Js-output.
That's it... Now you just need to create a nice frontend around the VMs.

Last edited Apr 2, 2014 at 1:24 PM by _RichardFrick, version 17