Knockout Js绑定和过滤两个数组 - 最佳实践

时间:2017-03-11 10:13:09

标签: javascript arrays knockout.js

我有两个模型定义如下:

var Card = function (id, name, detail, rowId) {

    this.id = ko.observable(id)
    this.name = ko.observable(name)
    this.detail = ko.observable(detail)
    this.rowId = ko.observable(rowId)
}

var Row = function (id, title, detail, cards) {

    this.id = ko.observable(id)
    this.title = ko.observable(title)
    this.detail = ko.observable(detail)
    this.cards = ko.observableArray([])
}

这里是行和卡阵列:

var cards = [
    {
      "id" : "-Ke__R4fK_OVSRAxZrjp",
      "detail": "More information about Card One",
      "name": "Card One",
      "rowid": "-KeFnIJLJtMz3RojUNx2",
      "url": "https://firebasestorage.googleapis.com/v0/b/leanlanes-becb2.appspot.com/o/card-images%2F142667555.jpg?alt=media&token=8d64656a-29d3-44b1-b62c-1232388a59c3"
    },
    {
      "id" : "-KedLYCo2L1dyNXqsrY2",
      "detail": "Nice little article",
      "name": "A piece of work",
      "rowid": "-KeFnIJLJtMz3RojUNx2",
      "url": "https://firebasestorage.googleapis.com/v0/b/leanlanes-becb2.appspot.com/o/card-images%2FA%20piece%20of%20yourself.jpg?alt=media&token=72aebfba-fdc1-4966-9272-e0103ba786a0"
    },
    {
      "id" : "-Keinb9kpnjyBAYNj1Co",
      "detail": "345ggg",
      "name": "New card 23",
      "rowid": "-KeFnIJLJtMz3RojUNx2",
      "url": ""
   }
]


var rows = [
    {
      "id" : "-KeFnIJLJtMz3RojUNx2",
      "detail" : "Some small info about this row",
     "title" : "This is the first row"
    },
    {
      "id" : "-KeFoCUybj0DcvYTiH1T",
      "detail" : "Some information about a subsequent row of info",
      "title" : "The second row"
    },
    {
      "id" : "-KeK04XQCnGweRifYDsd",
      "detail" : "Details about a row",
      "title" : "The row title"
    },
    {
      "id" : "-Ke_4jxrsdUw0-S0sv02",
      "detail" : "Will this row work? ",
      "title" : "Fourth Row"
   }
]

这是我的ViewModel:

var ViewModel = function (cards, rows) {
    // Row view bindings
    this.currentRowId = ko.observable()
    this.rows = ko.observableArray(rows.map(function (row) {
        return new Row(row.id, row.title, row.detail)
    }))
    // Card view bindings
    this.currentCardName = ko.observable()
    this.currentCardDetails = ko.observable()
    this.cards = ko.observableArray(cards.map(function (card) {
        return new Card(card.id, card.name, card.detail, card.rowid)
    }))

    this.setCards = function(rowid, cardsarray) {
        return ko.utils.arrayFilter(cardsarray, function (card) {
            return (card.rowid === rowid) // return card if matches rowId
        })
    }
}

我不明白将卡正确附加到行上的最佳方法。我尝试了很多不同的路线:过滤,在Row对象上调用一张新卡片......但我不确定最佳策略。

1 个答案:

答案 0 :(得分:1)

以下是我如何解决这一挑战。我试图使用包含其卡片的行设置我的ViewModel,基于将Card.rowId与Row.Id匹配。

var ViewModel = function (rows, cards) {

    // Setup Rows based on existing data
    var rowsWithCards = ko.utils.arrayMap(rows, function(row) {
        var cardStack = []
        cards.forEach(function (card) {
            if (card.rowid === row.id) {
                cardStack.push(card)
            }
        })
        return new Row(row.id, row.title, row.detail, cardStack)
    })

    // Row view bindings
    this.currentRowId = ko.observable()
    this.rows = ko.observableArray(rowsWithCards)
    this.cards = ko.observableArray(cards)
    // Card view bindings
    this.currentCardName = ko.observable()
    this.currentCardDetails = ko.observable()

}.bind(this)