Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would start with something like this:</p> <pre><code>$(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 }); </code></pre> <p>​</p> <p>Load Products into the cartItems observableArray in the CartViewModel.</p> <p>The dependent observable (ko.computed) value <code>eligibleForFreebies</code> will determine whether or not the Freebies should be allowed.</p> <p>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 <code>eligibleForFreebies</code> 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.)</p> <p>Hope this helps to get you started on this one, but let me know if you have any questions.</p> <p><strong>UPDATE:</strong> I <a href="http://jsfiddle.net/yUpWe/29/" rel="nofollow">forked your fiddle</a> and reworked your code a bit... well, I mostly moved it around, but I did add some functionality.</p> <p>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!</p> <p>If one adds a method for re-adding a subscription item to the cart, the freebies would reappear.</p> <p>Please have a look when you have a chance, and let me know if you'd like me to explain anything.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload