Search code examples
ajaxapostrophe-cms

Explanation about data-apos-ajax-append outside pieces-pages


I want to implement the apostrophes Ajax features in my project but sadly I don't understand the Ajax functions not completely. I have been studying the documentation for the last week now but i don't find enough information about that topic. I want kindly ask if it's possible to use data-apos-ajax-append outside of apostrophe-pieces-pages

I created a widget like that:

lib/modules/infinite-widgets/index.js

module.exports = {
  extend: 'apostrophe-widgets',
  label: 'Infinite Widget',
  addFields: [
    {
      name: 'posts',
      label: 'Posts',
      type: 'array',
      titleField: 'name',
      schema: [
        {
          name: 'name',
          type: 'string',
          label: 'Name'
        },
        {
          name: '_image',
          type: 'joinByOne',
          withType: 'apostrophe-image',
          label: 'Image',
          required: true,
          filters: {
            projection: {
              attachment: 1,
              description: 1,
              title: 1
            }
          }
        }
      ]
    }
  ]
};
lib/modules/infinite-widgets/views/widget.html

<div class="row margin">
  <div class="col s12 m12">
    <div class="radius
      {% if data.widget.shadowOn == true %}
        z-depth-1
      {% endif %}
    ">
      <div data-apos-ajax-context="infinite">
        {% include "indexAjax.html" %}
      </div>
    </div>
  </div>
</div>
lib/modules/infinite-widgets/views/indexAjax.html

<div data-apos-ajax-append>
  {% for name in data.widget.posts %}
    {% set image = apos.images.first(name._image) %}
    <div class="card z-depth-0" style="
        {%- if piece.backColor-%}
          background-color:{{ piece.backColor }}
        {% endif %}
      ">
      <div class="card-image">
        <img src="{{ apos.attachments.url(image, { size: 'one-sixth' }) }}" />
      </div>
      <div class="card-content">
      {{ name.name }}
      </div>
    </div>
  {% endfor %}
</div>

That is working pretty nice but unfortuantly It displays all posts created simultansiosly and I wasen't able to use the "Load More..." button with AJAX here

{# Load More button. Also outside data-apos-ajax-append, so it gets refreshed #}
{% if data.currentPage < data.totalPages %}
  {# "Load More" button with the "append=1" flag #}
  <a href="{{ data.url | build({ page: data.currentPage + 1, append: 1 }) }}">Load More...</a>
{% endif %}

So can I modify this query, to have a load more button, or even better data-apos-ajax-infinite-scroll for content of an array created in an widget instead of an piece?


Solution

  • No, it is not possible to do that using data-apos-ajax-append. That feature is specifically for apostrophe-pieces-pages and there is no back end implementation waiting to talk to it for widgets, for instance.

    You should instead look at writing a browser-side widget player for your widget, which gives you a place to make your own API calls back to the server. You can see that technique in the source of the apostrophe-twitter-widgets module.