Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating relationships between items in a Knockout.js model
    primarykey
    data
    text
    <p><strong>jsFiddle</strong>: <a href="http://jsfiddle.net/brandondurham/g3exx/" rel="nofollow">http://jsfiddle.net/brandondurham/g3exx/</a></p> <p><strong>How can I create relationships between various observableArrays in my model?</strong> For instance, in my model I have a <code>cartItems</code> array, and each item in that array has a nested <code>itemFreebies</code> array as well as an <code>itemType</code> property. Customers only get free items when they have a subscription in their cart (<code>"itemType" : "subscription"</code>) and, as such, when that subscription is removed I need to remove all other cart items' freebies, preferably with a nice fadeOut animation.</p> <p>What is the best way to create these types of conditional relationships?</p> <p>This is the object I'm using in my model:</p> <pre><code>{ "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 } ] } </code></pre>
    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. 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