Questions tagged [knockout-mapping-plugin]

Knockout.js Mapping is an open source Knockout.js plugin that simplifies the mapping a plain JavaScript object or JSON string to a Knockout view model.

Knockout.js Mapping, also known as ko-mapping, is an open source plugin that simplifies the mapping a plain JavaScript object or JSON string to a Knockout view model.

Without knockout-mapping

// declare view model and explicitly declare each observable property
var viewModel = {
    id: ko.observable(),
    name: ko.observable(),
    projects: ko.observableArray()
};

// get a response from the server in a variable `data`
{
    id: 1,
    name: 'Steve Sanderson',
    projects: ['knockout', 'knockout.mapping']
}

// apply server response to viewModel
viewModel.id(data.id);
viewModel.name(data.name);
viewModel.projects(data.projects);

With knockout-mapping

// declare view model
var viewModel = {};

// get a response from the server in a variable `data`
{
    id: 1,
    name: 'Steve Sanderson',
    projects: ['knockout', 'knockout.mapping']
}

// apply server response to viewModel
// this augments observable properties onto the viewModel and is configurable
viewModel = ko.mapping.fromJS(data);

Helpful Links

916 questions
202
votes
5 answers

KnockOutJS - Multiple ViewModels in a single View

I'm thinking that my application is getting quite large now, too large to handle each View with a single ViewModel. So I'm wondering how difficult it would be to create multiple ViewModels and load them all into a single View. With a note that I…
CLiown
  • 13,665
  • 48
  • 124
  • 205
86
votes
8 answers

Binding true / false to radio buttons in Knockout JS

In my view model I have a IsMale value that has the value true or false. In my UI I wish to bind it to the following radio buttons:
C.J.
  • 6,789
  • 7
  • 36
  • 45
56
votes
4 answers

Am I overusing the Knockout mapping plugin by always using it to do my viewmodel?

I'm still learning the proper usage of Knockout and I've found myself quickly getting away from ever typing ko.observable when setting up my viewmodel and instead just defining an object literal and passing it through the mapping plugin with…
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
42
votes
3 answers

Performance tuning a knockout application - guidelines for improving response times

I have a large, complex page that relies heavily on knockout.js. Performance is starting to become an issue but examining the call stack and trying to find the bottlenecks is a real challenge. I noticed in another question ( Knockout.js --…
Mark Robinson
  • 13,128
  • 13
  • 63
  • 81
41
votes
1 answer

Map JSON data to Knockout observableArray with specific view model type

Is there a way to map a JSON data object to an observable array and then in turn have each item of the observable array be initialized into a specific type of view model? I've looked at all of knockout's documentation along with the knockout and…
KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
35
votes
6 answers

Knockout.js Make every nested object an Observable

I am using Knockout.js as a MVVM library to bind my data to some pages. I'm currently building a library to make REST calls to a web service. My RESTful web service returns a simple structure: { id : 1, details: { name: "Johnny", …
frapontillo
  • 10,499
  • 11
  • 43
  • 54
25
votes
2 answers

KnockoutJS subscribe to property changes with Mapping Plugin

Is there anyway I can tell the knockout mapping plugin to subscribe to all property changes call a certain function? I realize I can manually subscribe to the property change event in this manner: var viewModel = { name:…
Gabe
  • 49,577
  • 28
  • 142
  • 181
24
votes
4 answers

Mapping deeply hierarchical objects to custom classes using knockout mapping plugin

Using the knockout mapping plugin ( http://knockoutjs.com/documentation/plugins-mapping.html ) can you map a deeply hierachical object? If I have an object with multiple levels: var data = { name: 'Graham', children: [ { …
Mark Robinson
  • 13,128
  • 13
  • 63
  • 81
23
votes
2 answers

How do I make a deep copy of a knockout object that was created by the mapping plugin

Here's my scenario. I'm using the knockout mapping plugin to create an observable viewmodel hierarchy for me. My hierarchy has nested elements in it. At a particular point in the hierarchy I want to put an Add button to insert a new blank copy of…
emirhosseini
  • 545
  • 2
  • 8
  • 17
20
votes
2 answers

Knockout Mapping Validation

I'm trying to attach validation to a mapped view. I'm using Knockout Mapping and Validation plugins. Pseudo-models: Person { int Id; string Name; Book[] Books; } Book { int Id; string Name; } Javascript: function viewModel() { …
19
votes
4 answers

Knockoutjs select with option group

Is there any way in Knockoutjs binding where I can specify optionsGroup ? something like follwoing