使用敲除映射将硬编码数据与服务器端生成的数据进行映射

Mapping hard coded data with server side generated data using knockout mapping

显示的代码 here is an modified version of an example of knockout-sortable.js by RP Niemeyer and created by lossleader as an answer

这里有一个数据模型如下..

var Table = function(id, name, students,maxstudents,allowedstudentgender) {
    this.students = ko.observableArray(students);
    this.students.id = id;
    this.name = ko.observable(name);
    this.students.maxstudents=ko.observable(maxstudents);
    this.students.allowedstudentgender= ko.observableArray(allowedstudentgender);
};

连续数据如下..

    var initialTables = [
        new Table(1,"Table One",  [
           new Student(3, "Jim", "male"),
             new Student(6, "Chase", "male")
        ],2,["male"])
//and so on..
    ];

数据在vm中初始化如下..

var SeatingChartModel = function(tables, availableStudents) {
    var self = this;
    this.tables = ko.observableArray(tables);

但是,当数据从服务器中获取并使用 knockout-mapping.js 映射到 knockout viewmodel 时,我所做的事情如下..

    var SeatingChartModel = function() {
            var self = this;
            this.tables = ko.observableArray();
    // more stuff happens here..and then..

     $.get('/api/gettables', function (data) {
        self.tables(ko.mapping.fromJS(data)()); // for jtoken
              OR
        self.tables(ko.mapping.fromJS(JSON.parse(data))()); // for json
    };

在这种情况下,我无法弄清楚从服务器发送的 json 的必要数据结构..

真诚感谢任何帮助..

谢谢

用arraymap解决了..

this.tables = ko.observableArray();
    var temptables= ko.utils.arrayMap(dataFromServer, function(item) {
    return new Table(item.id,item.name, item.students, item.maxstudents, item.allowedstudentgender);
        });
        this.tables(temptables);