Knockout.js - 如何在两个数组之间交叉引用对象

时间:2015-04-20 19:53:23

标签: knockout.js

我有2个型号:

var ProductModel = function(productid, productname, producttypeid, productprice) {
  var self = this;
  self.ProductId = productid; 
  self.ProductName = productname;
  self.ProductTypeId = producttypeid;
  self.ProductPrice = productprice;
}

var ProductTypeModel= function(producttypeid, producttypename) {
  var self = this;
  self.ProductTypeId = producttypeid; 
  self.ProductTypeName = producttypename;
}

我在viewmodel中使用它们:

var ProductViewModel = function() {
  var self = this;
  self.ProductList = ko.observableArray([]);
  self.ProductTypeList = ko.observableArray([]);

  //...init 2 array and some misc methods
}

在html文件中:

...
<table>
  <thead>
   <tr>
    <th>Id</th>
    <th>Type</th>
    <th>Name</th>
    <th>Price</th>
   </tr>
  </thead>
  <tbody data-bind="template: { name: 'row', foreach: ProductList }">
  </tbody>
</table>
...
<script id="row" type="html/template">
  <tr>
    <td data-bind="html: ProductId"></td>
    <td data-bind="html: ProductName"></td>
    <td data-bind="html: ProductTypeId"></td> <!-- I stuck here!!! -->
    <td data-bind="html: ProductPrice"></td>
  </tr>
</script>
...

我希望我的表显示ProductTypeName而不是ProductTypeId但我无法将ProductTypeId传递给任何函数以获取ProductTypeName。

2 个答案:

答案 0 :(得分:3)

var ProductViewModel = function() {
  var self = this;
  self.ProductList = ko.observableArray([]);
  self.ProductTypeList = ko.observableArray([]);

  //...init 2 array and some misc methods

  self.getProductTypeName = function (productTypeId) {
    var typeId = ko.unwrap(productTypeId),
        found = ko.utils.arrayFirst(self.ProductTypeList(), function (productType) {
           return ko.unwrap(productType.ProductTypeId) === typeId;
         });

    return found ? ko.unwrap(found.ProductTypeName) : null;
  };
};

<script id="row" type="html/template">
  <tr>
    <td data-bind="html: ProductId"></td>
    <td data-bind="html: ProductName"></td>
    <td data-bind="html: $root.getProductTypeName(ProductTypeId)"></td>
    <td data-bind="html: ProductPrice"></td>
  </tr>
</script>

答案 1 :(得分:1)

您有两个相互关联的独立集合。您可以创建一个模型,通过映射组合对象的计算可观察对象来管理该关联。

function ProductListModel (products, productTypes) {
    this.Products = ko.observableArray(
        ko.utils.arrayMap(products, function (product) {
            return new ProductModel(
                product.productId,
                product.productName,
                product.productTypeId,
                product.productPrice);
        })
    );
    this.ProductTypes = ko.observableArray(
        ko.utils.arrayMap(productTypes, function (productType) {
            return new ProductTypeModel(
                productType.productTypeId,
                productType.productTypeName);
        })
    );

    this.AssociatedProducts = ko.dependentObservable(function () {
        var products = this.Products(),
            productTypes = this.ProductTypes();
        return ko.utils.arrayMap(products, function (product) {
            var productType = ko.utils.arrayFirst(productTypes, function (productType) {
                return product.ProductTypeId === productType.ProductTypeId;
            });
            return {
                ProductId: product.ProductId,
                ProductName: product.ProductName,
                ProductType: productType.ProductTypeName,
                ProductPrice: product.ProductPrice,
            };
        });
    }, this);
}
function ViewModel(data) {
    var p = new ProductListModel(data.products, data.productTypes);
    this.Products = p.Products;
    this.ProductTypes = p.ProductTypes;
    this.AssociatedProducts = p.AssociatedProducts;
}
<tbody data-bind="template: { name: 'row', foreach: AssociatedProducts }">
</tbody>
...
<script id="row" type="html/template">
    <tr>
        <td data-bind="html: ProductId"></td>
        <td data-bind="html: ProductName"></td>
        <td data-bind="html: ProductType"></td>
        <td data-bind="html: ProductPrice"></td>
    </tr>
</script>

Fiddle