简体   繁体   English

Knockout.js基本数据绑定查询

[英]Knockout.js basic data-bind query

I'm fundamentally unable to grasp the knockout.js concept of data-binds so looking for newbie help. 我从根本上无法掌握数据绑定的kickout.js概念,因此需要新手帮助。

Funnily enough, the code is actually doing what I want it to. 有趣的是,代码实际上正在按照我想要的去做。 I just don't grasp why: 我只是不明白为什么:

HTML HTML

<div class = "gridStyle2" data-bind="koGrid: gridOptions"></div>

JS JS

    $.getJSON('http://127.0.0.1:8000/home/players/', function(playerdata) {

var self = this;
        this.myData = ko.observableArray(playerdata);
        this.mySelectedData = ko.observableArray([]);
        this.gridOptions = { data: self.myData,
                            columnDefs: [{ field: 'ShortName', displayName: 'Name', width: 100 },
                                         { field: 'CurrentVal', displayName: 'Value', width: 70 },
                                         { field: 'ShortClub', displayName: 'Club', width: 60 },
                                         { field: 'ShortPos', displayName: 'Position', width: 70 },
                                        ],

                            selectedItems: this.mySelectedData,

         };

ko.applyBindings(self, document.getElementById('newgrid'));


});

The issue 问题

So my JSON data is displayed in a "koGrid" quite nicely. 因此,我的JSON数据很好地显示在“ koGrid”中。 But when I try and duplicate the code to create a different table with a different JSON data-source, the tables do not display. 但是,当我尝试复制代码以使用不同的JSON数据源创建不同的表时,这些表将不会显示。

Should my data-bind be looking at "gridOptions"? 我的数据绑定应该查看“ gridOptions”吗? Shouldn't it be a variable? 应该不是变量吗? Whenever I have tried putting the above JS in a variable or function I fail badly. 每当我尝试将上述JS放入变量或函数中时,我都会失败。 For example: 例如:

New HTML 新的HTML

<div class = "gridStyle2" data-bind="koGrid: GetData()"></div>

JS using function JS使用功能

function GetData() {

$.getJSON('http://127.0.0.1:8000/home/players/', function(playerdata) {

var self = this;
        this.myData = ko.observableArray(playerdata);
        this.mySelectedData = ko.observableArray([]);
        this.gridOptions = { data: self.myData,
                            columnDefs: [{ field: 'ShortName', displayName: 'Name', width: 100 },
                                         { field: 'CurrentVal', displayName: 'Value', width: 70 },
                                         { field: 'ShortClub', displayName: 'Club', width: 60 },
                                         { field: 'ShortPos', displayName: 'Position', width: 70 },
                                        ],

                            selectedItems: this.mySelectedData,
                            multiSelect: false,
                            showFilter: false,
                            jqueryUITheme: true,
                            rowHeight: 22,
                            displaySelectionCheckbox: false,
         };

}

ko.applyBindings(GetData(), document.getElementById('newgrid'));


});

This doesn't work, and I've also tried using var BLah = $.getJSON, etc etc 这不起作用,我也尝试使用var BLah = $ .getJSON等

In my limited understanding of OOP, I thought calling GetData() would produce the same table? 在对OOP的有限了解中,我认为调用GetData()会产生相同的表? Can anyone point out where I've gone wrong? 谁能指出我哪里出问题了? I've tried copying the way the functions work in the knockout.js tutorials - http://learn.knockoutjs.com/#/?tutorial=collections 我已经尝试过在ockout.js教程中复制功能的工作方式-http: //learn.knockoutjs.com/#/? tutorial= collections

Thanks... 谢谢...

Rather than trying to explain how data-binds and binding contexts work exactly, I'll try to suggest how to refactor your code to make it easier to grasp: 与其尝试解释数据绑定和绑定上下文的工作原理,不如尝试建议如何重构代码以使其更容易理解:

Judging from your api call, you're attempting to show some sort of overview of players. 从您的api调用来看,您试图显示某种类型的播放器概述。 The overview has a list of players, and a list of selected players. 概述包含一个播放器列表和一个选定的播放器列表。 It also has a load method that queries an API and writes to the list. 它还具有一个load方法,该方法查询API并写入列表。

function PlayerOverview() {
  this.playerData = ko.observableArray([]);
  this.selectedPlayers = ko.observableArray([]);
};

PlayerOverview.prototype.loadPlayers = function() {
  $.getJSON('http://127.0.0.1:8000/home/players/', this.playerData);
};

To render a grid, we need grid options. 要渲染网格,我们需要网格选项。 Let's make a PlayerGrid : 让我们做一个PlayerGrid

function PlayerGrid(dataSource, selectionSource) {
  this.data = dataSource;
  this.selectedItems = selectionSource;

  this.columnDefs = [
    { field: 'ShortName', displayName: 'Name', width: 100 },
    { field: 'CurrentVal', displayName: 'Value', width: 70 },
    { field: 'ShortClub', displayName: 'Club', width: 60 },
    { field: 'ShortPos', displayName: 'Position', width: 70 },
  ];

  this.multiSelect = false;
  this.showFilter = false;
  this.jqueryUITheme = true;
  this.rowHeight = 22;
  this.displaySelectionCheckbox = false;
}

Now, we can add a grid to our main viewmodel: 现在,我们可以在主视图模型中添加网格:

function PlayerOverview() {
  this.playerData = ko.observableArray([]);
  this.selectedPlayers = ko.observableArray([]);

  // For rendering a grid UI:
  this.playerGrid = new PlayerGrid(this.playerData, this.selectedPlayers);
};

With this in place, your main app code will be: 完成此操作后,您的主要应用代码将为:

// Instantiate an overview
const playerApp = new PlayerOverview();

// Bind the UI
ko.applyBindings(playerApp);

// Make sure the data gets loaded
playerApp.loadPlayers();

The context of your view (html), is a PlayerOverview instance. 视图的上下文(html)是PlayerOverview实例。 All values referenced in data-binds should be properties of your viewmodel. 数据绑定中引用的所有值都应该是视图模型的属性。 For example: 例如:

<h1>
  Nr. of players: <span data-bind="text: playerData().length"></span>
</h1>
<h2>Grid:</h2>
<div data-bind="koGrid: gridOptions"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM