(关于使用Knockout插件)如何将子对象数组作为observablearray绑定到父observablearray
我对淘汰赛插件很陌生,我正在尝试用这个插件构建一个评论线程。当子评论绑定到父评论时,我遇到一些问题,预期的 json 字符串化对象应该如下所示;
[
{
CreatedBy: "user 1",
CreatedOn: Date(),
Description: "comment 1",
ChildFeeds: [
{
CreatedBy: "user 2",
CreatedOn: Date(),
Description: "comment 1-1"
},
{
CreatedBy: "user 3",
CreatedOn: Date(),
Description: "comment 1-2"
},
{
CreatedBy: "user 4",
CreatedOn: Date(),
Description: "comment 1-3"
}
]
},
{
CreatedBy: "user 5",
CreatedOn: Date(),
Description: "comment 2",
ChildFeeds: [
{
CreatedBy: "user 6",
CreatedOn: Date(),
Description: "comment 2-1"
},
{
CreatedBy: "user 7",
CreatedOn: Date(),
Description: "comment 2-2"
}
]
},
]
但是当我运行代码时,会得到以下数据而不是上面数据的结构。
[
{
"CreatedBy": "user 1",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1",
"ChildFeeds": [
{
"ChildFeeds": [
{
"CreatedBy": "user 2",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-1"
},
{
"CreatedBy": "user 3",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-2"
},
{
"CreatedBy": "user 4",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-3"
}
]
}
]
},
{
"CreatedBy": "user 5",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2",
"ChildFeeds": [
{
"ChildFeeds": [
{
"CreatedBy": "user 6",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2-1"
},
{
"CreatedBy": "user 7",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2-2"
}
]
}
]
}
]
在这里您会注意到,由于编码错误,不幸的是我为每个父对象复制了 ChildFeeds 对象数组两次。您可以从这里找到代码 http://jsfiddle.net/6qAmu/5/
实现此问题的最佳方法是什么?
谢谢
i'm quite new to knockout plugin and i'm trying to build a commentthread with this plugin. I've some issues when a child comment is bind to parent comment , the expected json stringified object should be some thing like below;
[
{
CreatedBy: "user 1",
CreatedOn: Date(),
Description: "comment 1",
ChildFeeds: [
{
CreatedBy: "user 2",
CreatedOn: Date(),
Description: "comment 1-1"
},
{
CreatedBy: "user 3",
CreatedOn: Date(),
Description: "comment 1-2"
},
{
CreatedBy: "user 4",
CreatedOn: Date(),
Description: "comment 1-3"
}
]
},
{
CreatedBy: "user 5",
CreatedOn: Date(),
Description: "comment 2",
ChildFeeds: [
{
CreatedBy: "user 6",
CreatedOn: Date(),
Description: "comment 2-1"
},
{
CreatedBy: "user 7",
CreatedOn: Date(),
Description: "comment 2-2"
}
]
},
]
but when i run the codes a get the below data instead of the structure of the above one.
[
{
"CreatedBy": "user 1",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1",
"ChildFeeds": [
{
"ChildFeeds": [
{
"CreatedBy": "user 2",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-1"
},
{
"CreatedBy": "user 3",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-2"
},
{
"CreatedBy": "user 4",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 1-3"
}
]
}
]
},
{
"CreatedBy": "user 5",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2",
"ChildFeeds": [
{
"ChildFeeds": [
{
"CreatedBy": "user 6",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2-1"
},
{
"CreatedBy": "user 7",
"CreatedOn": "Mon Jan 02 2012 15:50:51 GMT+0200 (Turkey Standard Time)",
"Description": "comment 2-2"
}
]
}
]
}
]
here you'll notice that because of wrong coding , unfortunately i duplicate the ChildFeeds object array twice for each parent object.. you can find the code from here http://jsfiddle.net/6qAmu/5/
what is the best approach to achieve this issue?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您的
ChildFeedModel
是一个包含ChildFeeds
observableArray 的对象,因此当您将其分配给wallfeedsmodel< 上的
ChildFeeds
属性时/code>,您最终会得到双ChildFeeds
。一种选择是让您的
ChildFeedsModel
实际上是一个 observableArray。因此,您将创建一个 observableArray,使用任何其他方法/属性扩展它,然后返回它。例如:
我将其更改为以小写字母开头,因为现在在创建一个时不需要 new 语句(它总是返回一个新的 observableArray)。
现在,在您的
wallfeedsmodel
中,您只需将其分配如下:示例:http:// jsfiddle.net/rniemeyer/xAKK5/
Right now your
ChildFeedModel
is an object that contains aChildFeeds
observableArray, so when you assign this to aChildFeeds
property on yourwallfeedsmodel
, you end up with the doubleChildFeeds
.One option is to have your
ChildFeedsModel
actually be an observableArray. So, you would create an observableArray, extend it with any additional methods/properties, then return it.Something like:
I changed it to start with a lower-case letter, because it would now not require a
new
statement when creating one (it always returns a new observableArray).Now, in your
wallfeedsmodel
you would just assign it like:Sample here: http://jsfiddle.net/rniemeyer/xAKK5/