【问题标题】:Pushing data from json object to ko.obsrvablearray()将数据从 json 对象推送到 ko.observablearray()
【发布时间】:2017-08-08 09:33:41
【问题描述】:

我想从 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 数组时,它可以工作 任何帮助表示赞赏。

【问题讨论】:

  • 你得到什么错误? taches 是对象数组,还是只是对象?
  • taches 只是对象
  • 如果taches 是一个对象,那么调用$.each 是没有意义的。你必须做self.data.push({ title: taches.type, releaseYear: taches.levels, director: taches.title });。如果这是实际修复,您的控制台中应该有一条错误消息...
  • @user3297291 非常感谢它的工作

标签: javascript arrays json object jet


【解决方案1】:
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);

【讨论】:

    【解决方案2】:

    感谢@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;
    });
    

    【讨论】:

    • 上述方法可以正常工作,但如果您以后要使用 forEach 循环,我建议进行一次调整。将 getJSON 结果推送到临时数组中,然后“一次”更新 self.data 可观察对象。这样做的原因是,正如您现在所拥有的那样,每次您进行推送时,淘汰赛 observable 都会发送一个事件。这可能会导致性能不佳。
    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2017-10-18
    相关资源
    最近更新 更多