Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:01:42+00:00 2026-06-05T02:01:42+00:00

When using the KO mapping plugin for a view model, then binding to a

  • 0

When using the KO mapping plugin for a view model, then binding to a select, the value is not set.

jsFiddle:

http://jsfiddle.net/JerryClinesmith/pCn9E/

HTML:

<h1>With ko.mapping (manager not set)</h1>
<div id="option-mapping">
    <select data-bind="options: availManagers, value: manager, optionsText: 'name', optionsCaption: 'Pick one'"></select>
    <div data-bind="text: json"></div>
</div>

<h1>Without ko.mapping (manager is set)</h1>
<div id="option-no-mapping">
    <select data-bind="options: availManagers, value: manager, optionsText: 'name', optionsCaption: 'Pick one'"></select>
    <div data-bind="text: json"></div>
</div>​

JavaScript:

var origData = {
manager: {},
availManagers: [
    {id: 1, name: 'Tom'},
    {id: 2, name: 'Joe'},
    {id: 3, name: 'James'}]
};

var ViewModel = function(data) {
    var self = this;

    ko.mapping.fromJS(data, {}, self);

    self.json = ko.computed(function() {
        return ko.toJSON(ko.mapping.toJS(self));
    });
};

var ViewModelNoMapping = function(data) {
    var self = this;

    self.manager = ko.observable(data.manager);
    self.availManagers = ko.observableArray(data.availManagers);

    self.json = ko.computed(function() {
        var obj = ko.toJS(self);
        delete obj.json;
        return ko.toJSON(obj);
    });
};

var vm = new ViewModel(origData);
var vm2 = new ViewModelNoMapping(origData);
ko.applyBindings(vm, document.getElementById('option-mapping'));
ko.applyBindings(vm2, document.getElementById('option-no-mapping'));​

A view model without the mapping plugin seems to work as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:01:44+00:00Added an answer on June 5, 2026 at 2:01 am

    I’ve hit the same problem before with a blank object, it doesn’t seem to create an observable….which means manager is a plain object, which means computed never triggers.

    eg, change it to… (note: I changed manager: {} to manager: ” which then gives it something to convert to an observable. Which then makes things tick along ok… also changed the convert to json function, as the other one didn’t seem to show the the values on manager.

     manager: '',
        availManagers: [
            {
            id: 1,
            name: 'Tom'},
        {
            id: 2,
            name: 'Joe'},
        {
            id: 3,
            name: 'James'}]
    };
    
    var ViewModel = function(data) {
        var self = this;
        //self.manager = ko.observable();
        ko.mapping.fromJS(data, {}, self);
        self.json = ko.computed(function() {
             var obj = ko.toJS(self);
            delete obj.json;
            return ko.toJSON(obj);
        });
    };
    

    I’m not sure if this is a bug in the mapping plugin, both with not converting to a observable, and also not converting to the correct json for display.

    and just to confirm, I downloaded the mapping plugin and wrote the test :-

    test('mapping an object creates a observable', function () {
        var data = {
            a : {}      
        };
    
        var mapped = ko.mapping.fromJS(data);  
      equal(ko.isObservable(mapped.a), true);
    });
    

    with the result

    failed
    Expected:   true
    Result:     false
    Diff:   true false 
    Source:     at Object.<anonymous> (file:///C:/OpenSourceReps/Knockout/mapping/spec/mappingBehaviors.js:1635:3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the mapping plugin to create my client side view model based on
I am using asp.net mvc 3 and knockoutjs 2.0 and mapping plugin I have
I'm using knockoutjs and the mapping plugin to generate view models from JSON returned
How can I map the value of an object collection using fluent mapping? I
Using knockout's mapping plugin, how would I go about displaying the information from and
We are using knockout and the knockout mapping plugin to facilitate databinding in our
I am using Knockout and the Knockout Mapping plugin. My MVC3 Action returns a
I am trying to do recursive templating on a tree using the ko.mapping plugin
I'm using the options binding on a select list used in a jQuery template:
I've been building out a page using ASP.NET MVC 2 using KnockoutJS, KnockoutJS Mapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.