KnockoutJS 选择模型

发布于 2025-01-03 04:10:07 字数 1848 浏览 1 评论 0原文

我正在尝试使用 KnockoutJS 绑定一对多映射,其中 1 个邮政编码可以有多个“代理”。我有以下类:

function CaseAssignmentZipCode(zipcode, agent) {
  var self = this;
  self.zipcode = ko.observable(zipcode);
  self.agent = ko.observable(agent);
}

function Agent(id, name) {
  var self = this;
  self.id = id;
  self.name = name;
}

function ZipcodeAgentsViewModel() {
  var self = this;
  self.caseAssignmentZipCodes = ko.observableArray([]);
  self.agents = ko.observableArray([]);

  jdata = $.parseJSON($('#Agents').val());
  var mappedAgents = $.map(jdata, function (a) { return new Agent(a.Id, a.Name) });
  self.agents(mappedAgents);

  var dictAgents = {};
  $.each(mappedAgents, function (index, element) {
    dictAgents[element.id] = element;
  });

  var jdata = $.parseJSON($('#CaseAssignmentZipCodes').val());
  var mappedZipcodeAgents = $.map(jdata, function (za) { return new CaseAssignmentZipCode(za.ZipCode, dictAgents[za.UserId], false) });
  self.caseAssignmentZipCodes(mappedZipcodeAgents);
}

var vm = new ZipcodeAgentsViewModel()
ko.applyBindings(vm);

我的绑定如下所示:

<table>
  <thead><tr><th>Zipcode Agents</th></tr></thead>
  <tbody data-bind="foreach: caseAssignmentZipCodes">
    <tr>
      <td><input data-bind="value: zipcode"></td>
      <td><select data-bind="options: $root.agents, value: agent, optionsText: 'name'"></select></td>
      <td><a href="#" class="image-button small delete-small no-text" data-bind="click: $root.removeZipcode">Remove</a></td>
    </tr>
  </tbody>
</table>

第一次一切都绑定良好,表和选择字段正确显示。但是,当我更改任何选择元素上的选定值时,什么也没有发生。我已将其他元素绑定到它们,但这些元素不会更新,并且我尝试使用 .subscribe() 来侦听更新事件,但这也不会触发。

我预计我建立/绑定这些关系的方式有问题,但我无法弄清楚如何挽救我的生命。

谢谢!

I'm trying to bind a 1-many mapping using KnockoutJS, where 1 zip code can have many 'agents'. I have the following classes:

function CaseAssignmentZipCode(zipcode, agent) {
  var self = this;
  self.zipcode = ko.observable(zipcode);
  self.agent = ko.observable(agent);
}

function Agent(id, name) {
  var self = this;
  self.id = id;
  self.name = name;
}

function ZipcodeAgentsViewModel() {
  var self = this;
  self.caseAssignmentZipCodes = ko.observableArray([]);
  self.agents = ko.observableArray([]);

  jdata = $.parseJSON($('#Agents').val());
  var mappedAgents = $.map(jdata, function (a) { return new Agent(a.Id, a.Name) });
  self.agents(mappedAgents);

  var dictAgents = {};
  $.each(mappedAgents, function (index, element) {
    dictAgents[element.id] = element;
  });

  var jdata = $.parseJSON($('#CaseAssignmentZipCodes').val());
  var mappedZipcodeAgents = $.map(jdata, function (za) { return new CaseAssignmentZipCode(za.ZipCode, dictAgents[za.UserId], false) });
  self.caseAssignmentZipCodes(mappedZipcodeAgents);
}

var vm = new ZipcodeAgentsViewModel()
ko.applyBindings(vm);

My bindings look like this:

<table>
  <thead><tr><th>Zipcode Agents</th></tr></thead>
  <tbody data-bind="foreach: caseAssignmentZipCodes">
    <tr>
      <td><input data-bind="value: zipcode"></td>
      <td><select data-bind="options: $root.agents, value: agent, optionsText: 'name'"></select></td>
      <td><a href="#" class="image-button small delete-small no-text" data-bind="click: $root.removeZipcode">Remove</a></td>
    </tr>
  </tbody>
</table>

Everything binds fine the first time, with the table and select fields appearing properly. However, nothing happens when I change the selected value on any of the select elements. I have bound other elements to them and these don't update, and I've tried using .subscribe() to listen for the update event, but this doesn't fire either.

I expect there's something wrong with the way I'm setting up/binding these relationships, but I can't figure it out to save my life.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2025-01-10 04:10:07

我认为您需要

self.agents = ko.observableArray([]); 

在 ZipcodeUsersViewModel 顶部添加

I think you need to add

self.agents = ko.observableArray([]); 

at the top of ZipcodeUsersViewModel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文