Search code examples
javascriptwebsproutcore

How do you bind the SC.Record.toMany() data of a SC.Record to a ListView?


I am attempting to render a UI using Sproutcore where I have a list of lists. There is a vertical list of data where each associated item has a list of items of its own. I've setup the models as I believe is appropriate based on what I know of Sproutcore.

Model:

App.VerticalData = SC.Record.extend({
  someField: SC.Record.attr(Boolean),
  associatedListOfData: SC.Record.toMany("App.HorizontalData", {
    inverse: "vertical",
    isMaster: YES
  })
});

App.HorizontalData = SC.Record.extend({
  someForeignKeyField: SC.Record.attr(Number),
  vertical: SC.Record.toOne("App.VerticalData", {
    inverse: "associatedListOfData",
    isMaster: NO
  }),
});

I've added a subView to the ListItemView of the vertical list and modified the subclass so that it will layout the items in the horizontal list...well...horizontally. The issue I have is that the only way I seem to be able to get the data from the toMany() relationship to bind to this horizontal list is to do the following for each item in the vertical list.

horizontalListView.set('content', App.store.find(SC.Query.local(App.HorizontalData, {
  conditions: 'someForeignKeyField = ' + keyValueOfVerticalItem)
})));

This is painfully, woefully slow. I don't understand why I can't seem to just use the results of the calculated property for the toMany() field for the current vertical list item. Can someone provide some guidance here? Perhaps I'm missing something silly.

If I try to access the property directly, this is the result I get back:

Object
SproutCore18001994610927067697: "sc1271"
__sc_super__: Object
_bindings: Array[0]
_kvo_cacheable: true
_kvo_changeLevel: 0
_kvo_changes: null
_kvo_cloned: Object
_kvo_content_observed_keys: Object
_kvo_dependents: Object
_kvo_revision: 2
_object_toString: "SC.ManyArray:sc1271"
_observableInited: true
_observers: Array[0]
_prevStoreIds: Array[167]
_properties: Array[15]
_records: null
bindings: Array[0]
manyAttribute: Object
outlets: Array[0]
propertyName: "associatedListOfData"
propertyRevision: 2
record: Object
recordType: App.HorizontalData
__proto__: Object

I don't know if this looks correct or not. So it is possible that I've got something still not defined properly in the FIXTURES I've used for my test data. The information I put in the associcatedListOfData field is an array of numbers that align to the GUIDs for the elements in the HorizontalData table.


Solution

  • After some more testing and experimentation I think the issue might be related to the App.store.dataHashes not containing the values for the associated records. This apparently must be manually populated for the association to reconcile the records and doesn't populate the store automatically whenever a SC.ManyArray is constructed in an association. I don't know if this is a bug in Sproutcore or if it is intended behaviour.

    I was able to resolve the issue by populating the this property using a query on the App.HorizontalData table that returned all the records I had generated for my FIXTURE. If the store has the hashed data available, using the SC.ManyArray returned as the content for a ListView worked as expected.