Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nicojs/knockouthelpers

Introducing DRY (Don't repeat yourself) principles and type safety to knockoutjs for ASP.NET MVC applications.
https://github.com/nicojs/knockouthelpers

Last synced: 25 days ago
JSON representation

Introducing DRY (Don't repeat yourself) principles and type safety to knockoutjs for ASP.NET MVC applications.

Awesome Lists containing this project

README

        

Knockout helper for ASP.NET MVC
===============================

Introducing DRY (Don't repeat yourself) principles and type safety to knockoutjs for ASP.NET MVC applications.

## Examples

### ViewModel class (C#):

```c#
public class UserJson : JsonViewModel
{
public UserJson()
{ }

public int Id { get; set; }
public string Name { get; set; }
public AccountProvider Account { get; set; }
public List Hobbies { get; set; }
}
public class HobbyJson : JsonViewModel
{
public string Name { get; set; }
public int Score { get; set; }
}
public enum AccountProvider
{
Facebook,
Google,
}
```

### Json:
```javascript
{"Id":1,"Name":"jan","Account":1,"Hobbies":[{"Name":"Coden","Score":10},{"Name":"Sporten","Score":5}]}
```

### Glue it all together:
```javascript
$(function () {
$.getJSON('/Home/User')
.done(function (gebruiker) {
$('.js-voorbeeldjson').html(JSON.stringify(gebruiker));
ko.applyBindings(ko.mapping.fromJS(gebruiker), $('.ko-gebruiker').element);
});
});
```

### Type safety in the view:
Using lambda's (like Html.TextboxFor, Html.LabelFor, etc)

This: ```Model.SpanWithBindingFor(g => g.ScreenName)```
Result: ``````

### DRY If / else

Type-save if-else and switch statements in your template. Changing a property name will result in a red build! Awesome!

This:
```c#
using (var stmnt = Ko.IfFor(g => g.Account, AccountProvider.Google))
{
google;
stmnt.Else();

facebook;
}
```

Result:
```html

google


facebook

```

## More

Download the source code and run the application. Play with it:
* ForEachFor()
* SwitchFor(Enum/String lambda)
* stmnt.Case(EnumType.EnumValue)
* Typesafe knockout expressies opstellen

## How does it work?

It uses another base view page for your application. It is defined in the web.config of your views folder:

```xml




```

Instead of retrieving the actual data in your view, you retrieve a knockout template:

```c#
return View(new KnockoutViewModel());
```

Use the KnockoutViewModel to define your template.
```c#
@model KnockoutViewModel
```

Use Ko.IfFor, Ko.ForEachFor, stmnt.Else, Ko.SwitchFor. For all C# statements there is a knockout template equivalent

## Still to do..

* Add clientside validation