knockoutjs 的数据加载模式
我想了解 KnockoutJS 是否适用于我的应用程序。我的数据模型(简化)如下:
function topic(data) {
this.id = data.id;
this.queries = ko.observableArray([]);
}
function query(data) {
this.id = data.id;
this.text = data.text;
this.searcher = data.searcherId;
this.postings = ko.observableArray([]);
}
function posting(data, query) {
this.documentId = data.docid;
this.rank = data.rank;
this.snippet = data.snippet;
this.score = data.score;
this.query = query;
this.document = null;
}
function document(data, topic) {
this.id = data.id;
this.url = data.url;
this.topic = topic;
}
对于给定的主题
,我有一个或多个查询
实例。每个查询都包含一个 posting
实例列表。每个帖子
都引用一个文档。只要posting
实例属于不同的query
实例,多个posting
就可以引用给定的document
。
如果发布
引用了一个新文档(尚未被任何查询
检索到),我想创建一个新实例;如果document
已经存在(id是唯一的),我想重新使用它。
我可以看到一些可能的替代方案来构造服务器返回的 JSON 数据:
- 在序列化帖子时,首先序列化所有文档的列表,然后用它们更新主文档列表。然后,发送包含文档 ID 引用的帖子。
- 将每个文档完全序列化为发布的属性,然后确定该条目是否多余。将非冗余条目添加到主列表中。
序列化数据的合理模式是什么?是否有一些映射插件魔法可以简洁地表达这一点?我可以控制生成 JSON 的服务器,并且可以以任何有意义的方式构建它。
谢谢,
吉恩
I am trying to understand if KnockoutJS will work for my application. My data model (simplified) is as follows:
function topic(data) {
this.id = data.id;
this.queries = ko.observableArray([]);
}
function query(data) {
this.id = data.id;
this.text = data.text;
this.searcher = data.searcherId;
this.postings = ko.observableArray([]);
}
function posting(data, query) {
this.documentId = data.docid;
this.rank = data.rank;
this.snippet = data.snippet;
this.score = data.score;
this.query = query;
this.document = null;
}
function document(data, topic) {
this.id = data.id;
this.url = data.url;
this.topic = topic;
}
For a given topic
, I have one or more query
instances. Each query contains a list of posting
instances. Each posting
refers to a document. More than one posting
can refer to a given document
as long as the posting
instances belong to different query
instances.
If a posting
refers to a new document (one not yet retrieved by any query
) I would like to create a new instance; if the document
already exists (ids are unique), I would like to re-use it.
I can see some possible alternatives for structuring the JSON data returned by the server:
- When serializing postings, first serialize a list of all documents, and update the master document list with them. Then, send postings with references to document ids.
- Serialize each document completely as a property of a posting, and then figure out if that entry is redundant. Add non-redundant entries to the master list.
What is a reasonable pattern for serializing the data? Is there some mapping plugin magic that would express this succinctly? I have control over the server that's generating the JSON, and can structure that in any way that makes sense.
Thanks,
Gene
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是我为实现选项 1 所做的工作:
我按如下方式调用此代码:
它负责创建新实例(通过
constructor
函数)并通过lookup< 查找现有实例。 /代码>。
Here's what I wound up doing to implement option 1:
I call this code as follows:
This takes care both of creating new instances (via the
constructor
function) and looking up existing ones vialookup
.查看entityspaces.js,有一个视频您可以观看,它支持完整的分层数据模型,甚至会生成您的WCF JSON服务,它也支持REST API。
的 Javascript ORM(数据访问)框架
使用 Knockout https://github.com/EntitySpaces/entityspaces 。 js#自述文件
Checkout entityspaces.js, there's a video you can watch, it supports full hierarchcial data models and will even generate your WCF JSON service, it supports REST API's as well.
A Javascript ORM (Data Access) Framework that uses Knockout
https://github.com/EntitySpaces/entityspaces.js#readme