简体   繁体   English

Knockout - 绑定计算机模型

[英]Knockout - Binding a computed from model

I'm trying to use a computed observable to create a custom div ID (eg branch3). 我正在尝试使用计算的observable来创建自定义div ID(例如branch3)。 However, anytime I attempt to bind the computed I get the "unable to parse bindings" error. 但是,任何时候我尝试绑定计算机我得到“无法解析绑定”错误。 I'm sure I could just go about doing this a different way, but I just don't understand why I can't use a computed here. 我确信我可以用不同的方式去做,但我只是不明白为什么我不能在这里使用计算。 I'm pretty sure I've seen it done. 我很确定我已经看过它了。

Here is the jsfiddle I've been working on. 这是我一直在努力的jsfiddle。

FIDDLE 小提琴

var branchList   =[{"Id":1,"Latitude":40.2444400000,"Longitude":-111.6608300000,"StreetAddress":"1525 W 820 N","BranchName":"GPS","City":"Cityplacwe","State":"UT","Zip":"84601"},{"Id":2,"Latitude":40.2455550000,"Longitude":-111.6616100000,"StreetAddress":"123 N Center","BranchName":"GPS Branch 2","City":"Lehi","State":"UT","Zip":"84043"}];

//myMarkers = new Array();

var Branch = function (data) {
  var self = this;
  self.Id = ko.observable(data.Id);
  self.Latitude = ko.observable(data.Latitude);
  self.Longitude = ko.observable(data.Id);
  self.BranchName = ko.observable(data.BranchName);
  self.StreetAddress = ko.observable(data.StreetAddress);
  self.City = ko.observable(data.City);
  self.State = ko.observable(data.State);
  self.Zip = ko.observable(data.Zip);
  this.DivId = ko.computed(function () {
    return self.Id();
  });
  //self.DivId = ko.computed({
  //  //Reading from object to field
  //  read: function () {
  //    return "branch" + self.Id();
  //  },
  //  //writing from field to object
  //  write: function (value) {

  //  }
//});

}

var BranchViewModel = function () {
  var self = this;

  //create knockout array
  self.branchArrayKO = ko.observableArray(branchList);
}

And the HTML 和HTML

<div data-bind="foreach: branchArrayKO">


<div data-bind="attr: {'id': DivId}">
  <p></p>
  <h2></h2>
  <ul>
    <li data-bind="text: Id"></li>
    <li data-bind="text: BranchName"></li>
    <li data-bind="text: StreetAddress"></li>
  </ul>
</div>

</div>

You need to convert your raw JavaScript array into an array of Branch es. 您需要将原始JavaScript数组转换为Branch es数组。 One way to do this is to use ko.utils.arrayMap to iterate over each item in the list and create a new Branch : 一种方法是使用ko.utils.arrayMap迭代列表中的每个项目并创建一个新的Branch

var BranchViewModel = function() {
    var self = this;

    //create knockout array
    self.branchArrayKO = ko.observableArray(ko.utils.arrayMap(branchList, function(branch) {
        return new Branch(branch);
    }));
}

Updated example: http://jsfiddle.net/hawMW/2/ 更新示例: http //jsfiddle.net/hawMW/2/

Another alternative that might be useful is the knockout mapping plugin , which you can use to automate all or part of the mapping process. 另一个可能有用的替代方案是knockout mapping plugin ,您可以使用它来自动执行全部或部分映射过程。

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

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