Knockout.js Make every nested object an Observable

后端 未结 6 1742
轻奢々
轻奢々 2020-12-04 22:00

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

6条回答
  •  被撕碎了的回忆
    2020-12-04 22:10

    I would use the knockout mapping plugin.

    var jsonData = {
        id : 1,
        details: {
            name: "Johnny",
            surname: "Boy"
        }
    }
    
    var yourMapping = {
        'details': {
            create: function(options) {
                return Details(options.data);
            }
        }
    }
    
    function Details(data) {
        ko.mapping.fromJS(data, {}, this);
    }
    
    function YourObjectName() {
        ko.mapping.fromJS(jsonData, yourMapping, this);
    }
    

    This will create your object hierarchy with all of the children as observables.

提交回复
热议问题