将数据从json对象推送到ko.obsrvablearray()

时间:2017-08-08 09:33:41

标签: javascript arrays json object jet

我想从看起来像那个

的json对象中推送一些字段
{
  "type": "tasks",
  "levels": 3,
  "links":    [
        {
            ....
        }   

     ],
     "assignedDate": "2017-08-02 16:03:36",
     "number": 200612,
     "priority": 3,
     "createdDate": "2017-08-02 16:03:36",
     "state": "ASSIGNED",
     "ownerRole": "LoanApplication.Process Owner",
     "processName": "LoanProcess",
     .
     .
     .

}
ko.observableArray 中的

。这是我的JS代码(我使用的是oracle jet)

define(['ojs/ojcore', 'knockout', 'ojs/ojtable'], function (oj, ko) {
    function homeContentViewModel() {
    var self = this;
    self.data = ko.observableArray();
    $.getJSON("http://localhost:8085/get/bpm").
            then(function (taches) {
                $.each(taches, function () {
                    self.data.push({
                        title: this.type,
                        releaseYear: this.levels,
                        director: this.title
                    });
                });
            });




    self.dataSource = new oj.ArrayTableDataSource(
            self.data, 
            {idAttribute: 'title'}
    );
    }
    return homeContentViewModel;
   });

ps:当我将JSON对象更改为JSON数组时,它可以工作 任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

function Model(opt_data) {
    var data        = opt_data || {};
    var self        = this;
    self.taches     = ko.observableArray([]); // with ([])

    for (var i = 0; i < data.taches.length; i++) {
        var tache = new Tache(data.taches[i]);
        self.taches.push(tache);
    }
}

function Tache(opt_data) { 
    var data = opt_data || {};
    var self = this;

    self.title       = ko.observable(data.type   || "");
    self.releaseYear = ko.observable(data.levels || "");
    self.director    = ko.observable(data.title  || "");
}

var vm = new Model(dataJson); //Your json with some data
ko.applyBindings(vm);

答案 1 :(得分:0)

感谢@ user3297291,这个工作

define(['ojs/ojcore', 'knockout', 'ojs/ojtable'], function (oj, ko) {
function homeContentViewModel() {
var self = this;
self.data = ko.observableArray();
$.getJSON("http://localhost:8085/get/bpm").
        then(function (taches) {

                self.data.push({
                    title: this.type,
                    releaseYear: this.levels,
                    director: this.title
                    });
        });




self.dataSource = new oj.ArrayTableDataSource(
        self.data, 
        {idAttribute: 'title'}
   );
   }
   return homeContentViewModel;
});