简体   繁体   English

Knockoutjs如何基于ID对可观察数组成员进行数据绑定

[英]Knockoutjs how to data-bind observable array members based on IDs

I'm not if the title explains what I need to achieve or not but I can change it later if some has a better suggestion. 我不是标题是否说明了我需要实现的目标,但如果有人有更好的建议,我可以稍后更改。

I'm using KO to manage a whole bunch of data on the client side. 我正在使用KO在客户端管理大量数据。

Here's the basic. 这是基本的。

  1. I have a list of training sessions 我有一份培训课程清单
  2. Each has a list of training session parts 每个都有培训部分的清单
  3. Each training session parts are referencing items kept in other lists. 每个培训部分都是参考其他列表中保留的项目。 For example, I have a list of activities (ex: biking, running, swimming, etc.) 例如,我有一个活动列表(例如:骑自行车,跑步,游泳等)
  4. Each activity is identified by an ID which is used in the training session parts to identify which activity was used for a particular session. 每个活动都由一个ID标识,该ID在培训课程各部分中使用,以标识在特定课程中使用了哪个活动。

Now, all these list are stored as observable arrays, and each member of the lists are observables (I use KO.Mapping to map the JSON coming from the server) 现在,所有这些列表都存储为可观察的数组,并且列表的每个成员都是可观察的(我使用KO.Mapping来映射来自服务器的JSON)

When I display a training session in my UI, I want to display various information coming from various lists 当我在用户界面中显示培训课程时,我想显示来自各种列表的各种信息

Duration: 1h30 时长:1h30
Activity: Biking 活动:骑自行车
Process: Intervals 工艺:间隔

The only information I have in order to link the training session to its component is an ID which is fine. 为了将培训课程与其组件链接起来,我唯一需要的信息就是ID。 What I'm not sure is how to data-bind the name (text) of my activity to a <p> or <div> so that the name will change if I edit the activity (by using some functionality of the application). 我不确定如何将活动名称(文本)数据绑定到<p><div>以便如果我编辑活动(通过使用应用程序的某些功能),名称将更改。

The training session only has the ID to identify the activity, so I don't know how to bind the name of the activity based on its ID. 培训课程仅具有识别活动的ID,因此我不知道如何根据其ID绑定活动的名称。

Hopefully this makes senses and someone can help me figure it out. 希望这有道理,有人可以帮助我解决。 I found lots of info on how to bind to observable array but nothing addressing ID and linked information. 我发现了很多有关如何绑定到可观察数组的信息,但没有解决ID和链接信息的问题。

The easiest way would probably be to make your own constructors and link the data by hand. 最简单的方法可能是创建自己的构造函数并手动链接数据。 You can use mapping if you really want to, but you'll basically have to do the same manual linking, only in a more verbose format. 如果确实需要,可以使用映射,但是基本上只需要更详细的格式就可以进行相同的手动链接。

This is the fiddle with the example implementation: http://jsfiddle.net/aKpS9/3/ 这是示例实现的摆弄: http : //jsfiddle.net/aKpS9/3/

The most important part of the code is the linking, you have to take care to create the activity objects only once, and use the same objects everywhere, as opposed to creating new activity objects for the parts. 该代码最重要的部分是链接,您必须注意仅创建一次活动对象,并在各处使用相同的对象,而不是为零件创建新的活动对象。

var TrainingSession = function(rawData, actualActivities){
    var self = this;
    self.name = ko.observable(rawData.name);
    self.parts = ko.observableArray(ko.utils.arrayMap(rawData.parts, function(rawPart){
        return ko.utils.arrayFirst(actualActivities(), function(ac){
            return ac.ID() == rawPart.ID;
        })
    }));
}

var Activity = function(rawData){
    var self = this;
    self.ID = ko.observable(rawData.ID);
    self.name = ko.observable(rawData.name);
}

var MainVM = function(rawData){
    var self = this;
    //first create an array of all activities
    self.activities = ko.observableArray(ko.utils.arrayMap(rawData.activities, function(rawAc){
        return new Activity(rawAc);
    }));

    self.trainingSessions = ko.observableArray(ko.utils.arrayMap(rawData.trainingSessions, function(session){
        return new TrainingSession(session, self.activities);
    }));

}

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

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