Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:07:08+00:00 2026-05-28T06:07:08+00:00

I am trying to understand if KnockoutJS will work for my application. My data

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:07:08+00:00Added an answer on May 28, 2026 at 6:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand why this doesn't work. I keep getting the following errors: left
Im trying to understand what a functor is, i found this tutorial/example: http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors data
I trying to understand this piece of code Function.prototype.method = function (name, fn) {
I am trying understand how multi queries work in mysqli. But I confess that
Im trying to understand when to call autorelease, and what this will actually do
Trying to understand the Android framework model. I have an application that needs to
Trying to understand Ruby a bit better, I ran into this code surfing the
I've been trying to understand how Ruby blocks work, and to do that I've
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
Trying to understand this binding process of the WPF. See the code at the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.