Search code examples
apostrophe-cms

Joined object not available in self.play


My widget joins an object with joinByOne, but the joined object is not available in self.play in public/js/always.js (only its id). It is available in views/widget.html though.

// index.js
{
  name: 'groups',
  type: 'array',
  schema: [
    name: 'buildings',
    type: 'array',                                 
    schema: [                                               
      {
        name: '_building',                              
        label: 'Building',                              
        type: 'joinByOne',                              
        withType: 'building',                           
        required: true,                                 
        filters: {                                      
          projection: {                               
            title:1,                                
            latitude:1,                             
            longitude:1,                            
            _url: 1                                 
          }                                           
        }                                               
      }
    ]
  ]
}

In views/widget.html joined objects are available, as expected.

// views/widget.html
<div class="map" data-groups='{{ data.widget.groups | jsonAttribute({ single:true }) }}'></div>

In the browser console $('.map').data('groups')[0].buildings[0]._building contains the joined object, as expected.

In self.play on the other hand, data.groups[0].buildings[0] contains the builgindId only, but no the actual _building.

// public/js/always.js
apos.define('map-widgets',{
  extend: 'apostrophe-widgets',
  construct: function(self,options) {
    self.play = function($widget,data,options) {
      console.log(data.groups[0].buildings[0])
    }
  }
})

This logs an object containing buildingId of the joined object, but no _building, i.e. the object itself.

It appears the join did not get executed in the object available in data in self.play. Is this on purpose?

How can I access the joined object in self.play?


Solution

  • Joins are filtered out of data by default but you can override the filtering function per widget and essentially bypass the filtering.

    In the index.js of your widgets module

    construct: function(self, options) {
       // ... other stuff
       self.filterForDataAttribute = function (widget) { 
          return widget;
       };
    }