Ich habe zwei Modelle wie folgt definiert:Knockout Js Bindung und Filterung zwei Arrays - Best Practice
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([])
}
Hier ist die Reihe und Kartenanordnungen:
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"
}
]
Hier ist meine 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
})
}
}
Ich verstehe nicht den besten Weg, um die Karten auf die Zeile richtig anzuhängen. Ich habe viele verschiedene Routen ausprobiert: Filtern, eine neue Karte auf dem Row-Objekt aufrufen ... aber ich bin mir nicht sicher über die beste Strategie.
Was meinen Sie mit dem Anhängen der Karten an die Reihe? Kannst du mehr Erklärungen geben? – muhihsan
Im Kartenmodell gibt es eine Eigenschaft "card.rowId", die die Zeilen-ID der Eltern enthält. Dies bedeutet, dass einer Karte bekannt ist, welcher Reihe sie angehört. Das Problem ist, dass ich ein Array von Karten habe, jede Karte mit einem Verweis auf die Eltern-Zeile und ein Array von Zeilen, aber ich weiß nicht, wie ich das im ViewModel richtig einstellen soll, damit jede Zeile ihr eigenes Array von Karten gefiltert hat von card.rowId? –