knockoutjs 的数据加载模式

发布于 2024-12-19 04:45:08 字数 1245 浏览 0 评论 0原文

我想了解 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 数据:

  1. 在序列化帖子时,首先序列化所有文档的列表,然后用它们更新主文档列表。然后,发送包含文档 ID 引用的帖子。
  2. 将每个文档完全序列化为发布的属性,然后确定该条目是否多余。将非冗余条目添加到主列表中。

序列化数据的合理模式是什么?是否有一些映射插件魔法可以简洁地表达这一点?我可以控制生成 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:

  1. 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.
  2. 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 技术交流群。

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

发布评论

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

评论(2

别靠近我心 2024-12-26 04:45:08

以下是我为实现选项 1 所做的工作:

function idField(data) {
    return ko.utils.unwrapObservable(data.id);
}

function createMapping(type, context) {
    return {
        key:    idField,
        create: constructor(type, context)
    }
}

function constructor(type, context) {
    return function(options) { 
        return new type(options.data, context); 
    }
}

function createReferenceMapping(collection) {
    return {
        key: idField,
        create: lookup(collection)
    }
}

function lookup(collectionOrClosure) {
    return function(options) {
        var collection = (typeof collectionOrClosure == 'function') ? collectionOrClosure() : collectionOrClosure;

        var object = collection.findById(options.data.idref);
        if (object == null)
            console.log("Error: Could not find object with id " + options.data.idref + " in ", collection);
        return object;
    }
}

我按如下方式调用此代码:

    var mapping = {
        people: createMapping(Searcher),
        topics: createMapping(Topic, this),
        activeTopic: createReferenceMapping(function(){return self.topics();})
    };

    this.dataChannel.loadModel(function(data) {
        ko.mapping.fromJS(data, mapping, this);
    }

它负责创建新实例(通过 constructor 函数)并通过 lookup< 查找现有实例。 /代码>。

Here's what I wound up doing to implement option 1:

function idField(data) {
    return ko.utils.unwrapObservable(data.id);
}

function createMapping(type, context) {
    return {
        key:    idField,
        create: constructor(type, context)
    }
}

function constructor(type, context) {
    return function(options) { 
        return new type(options.data, context); 
    }
}

function createReferenceMapping(collection) {
    return {
        key: idField,
        create: lookup(collection)
    }
}

function lookup(collectionOrClosure) {
    return function(options) {
        var collection = (typeof collectionOrClosure == 'function') ? collectionOrClosure() : collectionOrClosure;

        var object = collection.findById(options.data.idref);
        if (object == null)
            console.log("Error: Could not find object with id " + options.data.idref + " in ", collection);
        return object;
    }
}

I call this code as follows:

    var mapping = {
        people: createMapping(Searcher),
        topics: createMapping(Topic, this),
        activeTopic: createReferenceMapping(function(){return self.topics();})
    };

    this.dataChannel.loadModel(function(data) {
        ko.mapping.fromJS(data, mapping, this);
    }

This takes care both of creating new instances (via the constructor function) and looking up existing ones via lookup.

黎歌 2024-12-26 04:45:08

查看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

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