加入的对象在 self.play 中不可用

Joined object not available in self.play

我的小部件与 joinByOne 连接了一个对象,但是连接的对象在 public/js/always.js 中的 self.play 中不可用(只有它的 ID)。它在 views/widget.html 中可用。

// 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                                 
          }                                           
        }                                               
      }
    ]
  ]
}

views/widget.html 中,连接对象可用,正如预期的那样。

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

在浏览器控制台中 $('.map').data('groups')[0].buildings[0]._building 包含已连接的对象,正如预期的那样。

另一方面,在 self.play 中,data.groups[0].buildings[0] 仅包含 builgindId,但没有实际的 _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])
    }
  }
})

这会记录一个对象,其中包含 buildingId 个已连接对象,但没有 _building,即对象本身。

似乎在 self.playdata 中可用的对象中没有执行连接。这是故意的吗?

如何访问 self.play 中的连接对象?

默认情况下,连接会从 data 中过滤掉,但您可以覆盖每个小部件的过滤功能,基本上绕过过滤。

在您的小部件模块的 index.js

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