多种型号

发布于 2025-01-05 07:03:57 字数 470 浏览 1 评论 0原文

我喜欢 knockoutjs,我们越早摆脱直接针对 DOM 的编码就越好。我无法理解我将如何做一些我将用问题/答案网站来解释的事情。 (这可能是一个一般的 MVC/MVVM 问题)

在我的数据模型中,我有一个问题 [id,描述] 和答案 [id,question_id,文本]。浏览器请求绑定到正文的问题列表,一列将显示问题描述,而另一列应绑定到答案文本框。

一种明显的方法是使用 QuestionAnswer[question_id,answer_id,question_descrition,answer_text] 模型。理想情况下,我希望将它们分开,以最大程度地减少发送/接收/存储时的转换,如果没有某种方法将它们分开,那么我有以下问题:

创建 QuestionAnswer 模型的理想位置在哪里?我敢打赌,按照惯例,它是在服务器上创建的。

如果某个地方有这样的例子,请指出我,否则我认为这将是一个很好的例子。

请帮我解决这个问题,谢谢!

I like knockoutjs, the sooner we get rid of coding directly toward the DOM the better. I'm having trouble understanding how I would do something which I'm going to explain in terms of a question/answer site. (This is probably a general MVC/MVVM question)

In my data model I have a question[id, description] and answer[id, question_id, text]. The browser requests a list of questions which is bound to a tbody, one column will display the question description, while the other should be bound to an answer textbox.

One obvious way of doing this is to have a QuestionAnswer[question_id, answer_id, question_descrition, answer_text] model. Ideally I'd like to keep them separate to minimize transformation when sending/receiving/storing, if there isn't some way of keeping them separate then I have the following question:

Where is the ideal place to create the QuestionAnswer model ? My bet is that by convention its created on the server.

If there is such an example somewhere please point me to it, otherwise I think it would make a good example.

Please help me wrap my head around this, Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2025-01-12 07:03:57

您可以做的是在服务器上创建组合模型,将其序列化为 json,然后使用映射插件将序列化列表添加到视图模型中。

我正在这里 只是它不是组合模型,但应该没有任何区别。特别是因为你们的关系似乎是一对一的。

如果您需要在视图模型中创建“对象”,则可以使用映射定义来执行此操作,就像我一样 此处

我使用 C# 构建我的 模型在服务器上,但我想你可以使用任何你觉得舒服的东西。

映射插件最酷的一点是它将数据添加到视图模型中,以便您可以专注于行为。

What you could do is to create the combined model on the server, serialize it to json and then use the mapping plugin to add the serialized list to the view model.

I'm doing that here only it isn't a combined model, but shouldn't make any difference. Especially since it seems like your relation is one-to-one.

If you need to create an "object" in your view model, you can use the mapping definition to do so, like I do here.

I use C# to build my model on the server, but I guess you can use whatever you are comfortable with.

The cool thing with the mapping plugin is that it adds the data to the view model so that you can focus on behaviour.

紫罗兰の梦幻 2025-01-12 07:03:57

好的,
我已经收集了我的想法,了解我的问题实际上在问什么。
要在客户端进行数据绑定,您显然还需要那里的数据模型。我对需要发送什么以及何时发送感到矛盾。

继续讨论问题/答案站点的想法:发送一个答案列表,每个答案都包含一个问题,这就是应该做的事情。这样,您可以绑定到答案列表,并将每个答案的问题描述简单地绑定到第一个表列。

如果稍后我想制作一个问题编辑器,我可能会发送一个完全不同的数据结构,并且不会重用具有先前使用过的问题结构的答案。

我认为可能有一种方法可以发送引用自身的更复杂的数据结构。这显然在 JSon 中可以通过一些额外的库实现。

Ok,
I'v gathered my thoughts on what my question is actually asking.
To do data binding on the client side you obviously need your data model there as well. I was conflicted on what I needed to send over and at what time.

To continue with the Question/Answer site idea: Sending down a list of answers each of which have a question in them is what should be done. That way you can bind to the answer list and simply bind the question description of each answer to the first table column.

If later I want to make a question editor I would potentially send a complete different data structure down and not reuse the Answer has a Question structure previously used.

I thought there might be a way of sending down a more complex data structure that references itself. Which apparently is possible in JSon with some extra libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文