The best way to use JsMapper with ASP.NET MVC, Web API, SignalR and other, great Frameworks

JsMapper was built the way that it fits typical ASP.NET (MVC / Web API)-project structures.

The Domain-Classes / Models

If you already have made your Data-Models, most things are already done for this step.
All you need to do, it to add the JsModelAttribute.
It allows you to
  • Add a JsNamespace. If you would like to use the same JsNamespace for all your models, read Step 2 first
  • Define the Server-Url. This is necessary so the model knows where the responsible implementation of GET/POST/PUT/DELETE is
  • Set the model name. If the name should be the same as the C#-model, you don't need to fill in this field

public class Employee
    public int Id { get; set; }

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

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

    public string EmailAddress { get; set; }

If you don't want to expose a certain property, just add the IgnoreForJsModelAttribute.

Configurate the JsModelManager

The JsModelManager needs to be configured, before you can get JS-code.
But don't worry, the configuration-process is very easy.

You can do it by passing a JsModelConfig instance to the JsModelManager.ConfigMngr.Configurate-Method.

If you want, you can use the JsModelFluentConfig.
With it, you can create the JsModelConfig with meaningful method-names.
Here's an example:
    new JsModelFluentConfig()

Add Models without Auto-Scanning

If your value of JsModelConfig.ScanJsModels is false, you have to add the Models manually.
But that's almost as easy as letting the Scanner do all the work:

Getting the ViewModel-Code

You can either get a specific ViewModel-Code, or you can get for all scanned/from you added JsModel-Definitions:
var empModelOut = JsModelManager.GetJsOutput<Employee, Backbone>();
var allModelOut = JsModelManager.GetAllJsOutput<Backbone>();

You can choose between Backbone and KnockoutJs.
Or you can add your own Mapper. If you want to do so, check out the "creating JsModelMapper-Classes"-page.

Last edited Jul 3, 2013 at 4:42 PM by _RichardFrick, version 4


No comments yet.