Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ideally, you should have only at most one copy of any object within the same view model. If you can help it, refactor your view model so your objects can reference an array/map containing all objects to minimize replication. Then your actual objects simply need to get the referenced objects.</p> <pre><code>var model = { _FooRefs: [99, 100], _PooRefs: [99], _AllObjects: [ { ID: 99, Title: 'This is item number ninety-nine', Description: 'This is an item included in more than one object' }, { ID: 100, Title: 'This is item number one-hundred', Description: 'This is an item included in only one object' } ] }; </code></pre> <pre><code>function ViewModel(data) { var self = this; ko.mapping.fromJS(data, { _AllObjects: { key: function (obj) { return obj.ID; } } }, self); self.Foo = ko.computed(function () { var fooRefs = ko.utils.unwrapObservable(self._FooRefs); return ko.utils.arrayMap(fooRefs, function (id) { // mappedGet was added to mapping plugin version 2.4.1 return self._AllObjects.mappedGet({ ID: id }); }); }); self.Poo = ko.computed(function () { var pooRefs = ko.utils.unwrapObservable(self._PooRefs); return ko.utils.arrayMap(pooRefs, function (id) { return self._AllObjects.mappedGet({ ID: id }); }); }); } </code></pre> <p><a href="http://jsfiddle.net/jeff1203/XZGek/" rel="nofollow">fiddle</a></p> <p>To add objects to the array, you need to ensure that the object exists in your array/map and then add the object's key to the array of references. To remove an item, remove the reference and optionally remove the object.</p> <hr> <p>If doing this is not an option and you'll need to keep the duplication, you can probably use the <a href="https://github.com/rniemeyer/knockout-postbox/" rel="nofollow">postbox</a> plugin to help keep the objects synchronized. This approach would be best if the objects exist in separate view models.</p>
    singulars
    1. This table or related slice is empty.
    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