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

  • SEARCH
  • Home
  • 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 8431855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:58:08+00:00 2026-06-10T05:58:08+00:00

jsFiddle : http://jsfiddle.net/brandondurham/g3exx/ How can I create relationships between various observableArrays in my model?

  • 0

jsFiddle: http://jsfiddle.net/brandondurham/g3exx/

How can I create relationships between various observableArrays in my model? For instance, in my model I have a cartItems array, and each item in that array has a nested itemFreebies array as well as an itemType property. Customers only get free items when they have a subscription in their cart ("itemType" : "subscription") and, as such, when that subscription is removed I need to remove all other cart items’ freebies, preferably with a nice fadeOut animation.

What is the best way to create these types of conditional relationships?

This is the object I’m using in my model:

{
    "cartItems" : [
        {
            "itemName" : "Product 1",
            "itemDesc" : "Product 1 description",
            "itemType" : "subscription",
            "itemPrice" : 299,
            "itemFreebies" : false
        }, {
            "itemName" : "Product 2",
            "itemDesc" : "Product 2 description",
            "itemType" : "desktop",
            "itemPrice" : 4499,
            "itemFreebies" : [{
                "freebieName" : "Product 2 freebie",
                "freebieDesc" : "Product 2 freebie description",
                "freebieOriginalPrice" : 99
            }]
        }, {
            "itemName" : "Product 3",
            "itemDesc" : "Product 3 description",
            "itemType" : "desktop",
            "itemPrice" : 8999,
            "itemFreebies" : [{
                "freebieName" : "Product 3 freebie",
                "freebieDesc" : "Product 3 freebie description",
                "freebieOriginalPrice" : 99
            }]
        }, {
            "itemName" : "Product 4",
            "itemDesc" : "Product 4 description",
            "itemType" : "desktop",
            "itemPrice" : 99,
            "itemFreebies" : [{
                "freebieName" : "Product 4 freebie",
                "freebieDesc" : "Product 4 freebie description",
                "freebieOriginalPrice" : 99
            }]
        }, {
            "itemName" : "Product 5",
            "itemDesc" : "Product 5 description",
            "itemType" : "webfont",
            "itemPrice" : 49,
            "itemFreebies" : false
        }
    ]
}
  • 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-06-10T05:58:10+00:00Added an answer on June 10, 2026 at 5:58 am

    I would start with something like this:

    $(function () {
        ​var CartViewModel = {
            var self = this;
    
            self.cartItems = ko.observableArray([]);
    
            self.eligibleForFreebies = ko.computed(function() {
                return ko.utils.arrayFirst(self.cartItems(), function(cartItem) {
                    // I forgot the () after itemType in the original post
                    return (cartItem.itemType() === 'subscription');
                });
                // Note: ko.utils.arrayFirst will either return the item in 
                //      question, or it will return undefined or null… 
                //      I forget which, but either will result in 
                //      eligibleForFreebies evaluating to false
            });
        };
    
        var Product = function() {
            var self = this;
    
            self.itemName = ko.observable();
            self.itemDesc = ko.observable();
            self.itemType = ko.observable();
            self.itemPrice = ko.observable();
            self.itemFreebies = ko.observableArray([]);
        };
    
        var Freebie = function() {
            var self = this;
    
            self.freebieName = ko.observable();
            self.freebieDesc = ko.observable();
            self.freebieOriginalPrice = ko.observable();
        }
    
        ko.applyBindings(new CartViewModel());
        // load data
    });
    

    ​

    Load Products into the cartItems observableArray in the CartViewModel.

    The dependent observable (ko.computed) value eligibleForFreebies will determine whether or not the Freebies should be allowed.

    It’s likely that you wouldn’t even need to remove the freebies from the Products when the cart is not eligible for them – simply check eligibleForFreebies and include or exclude the freebies from the display, invoice, etc. (This might save you the headache of retrieving freebies after the user adds the subscription, but I suppose that depends on your scenario.)

    Hope this helps to get you started on this one, but let me know if you have any questions.

    UPDATE: I forked your fiddle and reworked your code a bit… well, I mostly moved it around, but I did add some functionality.

    Notice that if you delete the subscription item from the cart, all the freebies disappear from the cart display–but I didn’t delete them from the objects!

    If one adds a method for re-adding a subscription item to the cart, the freebies would reappear.

    Please have a look when you have a chance, and let me know if you’d like me to explain anything.

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

Sidebar

Related Questions

I have some sort of elsif statment bug I can't find: My JSfiddle: http://jsfiddle.net/z3xV3/64/
I have created this jsfiddle: http://jsfiddle.net/mfnxm/1/ I am trying to create this JSON: [{
as you can see on jsfiddle http://jsfiddle.net/C8B8g/7/ I have 2 button (= 2 divs)
jsFiddle : http://jsfiddle.net/brandondurham/g3exx/ I'm working on a shopping cart setup and using the following
Hi I have setup the following jsFiddle http://jsfiddle.net/a9ugu/2/ I am wondering why I get
Have a look at this jsfiddle: http://jsfiddle.net/Dh96F/1/ The Expand button will animate the widening
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
I have 2 internal div's within a main div JSfiddle - http://jsfiddle.net/uwdnH/ Illustration -
So I have come up with the following style sheet in jsfiddle http://jsfiddle.net/8FNZE/2/ and
I have two select boxes with dates. See here the jsfiddle: http://jsfiddle.net/egrba/ The people

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.