JavaScript Knockout mapping - difference between objects and observables in class being mapped to

This is driving me absolutely insane, and I just cannot see what I have done wrong. Please help before I start bibbling and gnawing my colleagues leg off. He doesn't deserve that.

I have an object I am mapping, which has a property that can or cannot contain an object of that same type. That is the only level of nesting there is. It is very simple; it is complicated only by the fact that the object is calling a base class constructor to set some default behaviour.

This base class sets up all the fields that can appear in the model (it is a generated file) and then maps the datasource, if it has one.

The mapping of the nested field to the correct constructor works if the field is set up initially as an observable. It does not if it is set up as a plain object.

var NS = {};
var _itest = 0;

NS.FieldModelBase = function(data, mapping)
{
    var _this = this;

    this.Text = ko.observable();

    // DOES NOT WORK 
    this.AlternateField = {};       

    // WORKS
    //this.AlternateField = ko.observable();       

    ko.mapping.fromJS(data, mapping, _this);
};

// =====================================================================
NS.FieldModel = function(data, mapping, parent)
{
    var _this = this;   

    window.console && console.log('CREATING FIELD', data); 

    var _mapping = $.extend({}, mapping, {
        'include': [ 'Test' ],
        'AlternateField': {
            create: 
                function(o) 
                { 
                    window.console && console.log('FOUND SUBFIELD', o.data);

                    return o.data ? new NS.FieldModel(o.data) : null;
                }
        }
    });

    this.Test = ko.observable(_itest++);

    NS.FieldModelBase.call(_this, data, _mapping);
}       

// =====================================================================
var model = new NS.FieldModel({
    Text: "Main option",
    AlternateField: {
        Text: "Alternate option",
        AlternateField: null
    }
}, { include: [ 'Test' ] });

ko.applyBindings(model);

https://jsfiddle.net/whelkaholism/fkr0w98u/

So, when setup as an object, printing model out after running the code gives:

{"Test":0,"Text":"Main option","AlternateField":{"Text":"Alternate option","AlternateField":null}}

There is no Test property on the alternate field. If you check the console, what happens is that the mapping create is in fact called, but the o.data property is null.

Change to an observable, and the output is, as expected:

{"Test":0,"Text":"Main option","AlternateField":{"Test":1,"Text":"Alternate option","AlternateField":null}}

So, what is the mapping plugin doing here? It was my understanding that it would map everything in source data, regardless of the existence or type of any existing properties on the object?

EDIT: I have solved my immediate problem with this change:

NS.FieldModel = function(data, mapping, parent)
{
    var _this = this;   

    var _mapping = {
        copy: [ 'AlternateField' ]
    };

    NS.FieldModelBase.call(_this, data, _mapping);

    this.AlternateField = data.AlternateField ? new NS.FieldModel(data.AlternateField, null, _this) : null;
}       

This manually creates the correct object type for the alternate field after the mapping. The copy directive in the mapping is absolutely required, or the newly created object has no properties mapped.

I don't now why this is, so I'm still looking for the answer on why the mapping plugin works differently depending on the content of pre-existing variables, because I despise having code that I don't know exactly why it works!

Answer:1

I currently have a table and each cell has a button. Upon clicking the button, based on that particular day (Monday or Tuesday), class (class 1 or class 2), and name (Kev or Josh), how can I push an ...

I currently have a table and each cell has a button. Upon clicking the button, based on that particular day (Monday or Tuesday), class (class 1 or class 2), and name (Kev or Josh), how can I push an ...

Lets say I had a HTML5/JS based application that is wrapped in cordova phone gap. Can I have it startup and download additional html5/js files? Basically the app would work with the base files, but ...

Lets say I had a HTML5/JS based application that is wrapped in cordova phone gap. Can I have it startup and download additional html5/js files? Basically the app would work with the base files, but ...

I have a form with multiple TextBoxes, Textarea and Buttons. By pressing the ENTER in any text box submits the form. So i want to disable the pressing enter only in text boxes and to enable in text-...

I have a form with multiple TextBoxes, Textarea and Buttons. By pressing the ENTER in any text box submits the form. So i want to disable the pressing enter only in text boxes and to enable in text-...

  1. disable enter submit form
  2. disable enter submit form angular
  3. disable enter submit form javascript
  4. disable enter submit form jquery
  5. disable enter submit
  6. disable enter submit form html
  7. disable enter submit jquery
  8. disable enter submit form react
  9. disable enter submit form asp.net
  10. disable enter submit angular
  11. disable submit enter key
  12. html disable enter submit
  13. disable enter key submit form
  14. input disable enter submit
  15. js disable enter submit
  16. formik disable enter submit
  17. javascript disable enter submit
  18. disable press enter submit form
  19. rails disable enter submit
  20. mvc disable enter submit

I am dealing with jQuery animation with infinite loop and my code is below: var width = $(document).width() - $('#mydiv').width(); $(document).ready(function() { function animateMydiv() { ...

I am dealing with jQuery animation with infinite loop and my code is below: var width = $(document).width() - $('#mydiv').width(); $(document).ready(function() { function animateMydiv() { ...

  1. create infinite loop python
  2. create infinite loop in c
  3. create infinite loop gif
  4. create infinite loop video
  5. create infinite loop javascript
  6. create infinite loop bash
  7. create infinite loop in java
  8. create infinite loop powershell
  9. create infinite loop
  10. create infinite loop batch file
  11. create infinite loop in jquery
  12. create infinite loop shell script
  13. create infinite loop linux
  14. create infinite loop in excel
  15. create infinite loop vba
  16. create infinite loop in php
  17. create infinite loop in abap
  18. create infinite loop batch
  19. create infinite loop program
  20. create infinite folder loop