Exercise 8: Customizing Pattern

Warning

This exercise requires a working buildout using a fork of the collective.jstraining package.

In this exercise, we will be walking through customizing the livesearch pattern.

You will be working in the exercise8 directory of the collective.jstraining package.

Add Your Pattern File

In your exercise8/static directory, add a file named pattern.js. Use this file to build your pattern.

This example will define a new pattern to overwrite the existing livesearch pattern:

/* global require */

require([
  'jquery',
  'mockup-patterns-livesearch',
  'pat-registry'
], function($, Livesearch, registry) {
  'use strict';
  /* combining bundle and pattern in same file this example */


  // first, unregister existing pattern
  delete registry.patterns.livesearch;
  delete $.fn.patLivesearch;


  // creating new pattern automatically registers it
  Livesearch.extend({
    name: 'livesearch',
    trigger: '.pat-livesearch',
    parser: 'mockup',
    init: function() {
      var that = this;
      Livesearch.prototype.init.call(that);

      // all we are doing in this customization is defaulting to searching
      // current section
      $('.searchSection input', that.$el)[0].checked = true;
    }
  });

});

Pay close attention to what we are doing here:

(
...
delete registry.patterns.livesearch;
delete $.fn.patLivesearch;
...
)

We are deleting the existing registration of the livesearch pattern.

Next, we are extending the existing pattern:

(
...
Livesearch.extend({
...
)

And overriding the init function to provide our customization(default search current section):

(
 ...
 $('.searchSection input', that.$el)[0].checked = true;
 ...
)

Register Static Resource Directory

Next, let us register the static directory we just placed our script into. To register it, you need to add ZCML registration for the static directory your script is in.

Add this to the exercise8/configure.zcml file:

<plone:static
    directory="static"
    type="plone"
    name="exercise8"
    />

Register Your Bundle

Again, registration is done exactly the same as previous exercises:

<?xml version="1.0"?>
<registry>

  <records prefix="plone.resources/exercise8"
           interface='Products.CMFPlone.interfaces.IResourceRegistry'>
    <value key="js">++plone++exercise8/pattern.js</value>
    <value key="css">
      <element>++plone++exercise8/pattern.less</element>
    </value>
  </records>

  <records prefix="plone.bundles/exercise8"
            interface='Products.CMFPlone.interfaces.IBundleRegistry'>
    <value key="resources">
      <element>exercise8</element>
    </value>
    <value key="merge_with">default</value>
    <value key="enabled">True</value>
    <value key="jscompilation">++plone++exercise8/exercise8-compiled.min.js</value>
    <value key="csscompilation">++plone++exercise8/exercise8-compiled.css</value>
    <value key="last_compilation">2016-10-04 00:00:00</value>
    <value key="stub_js_modules">
      <element>jquery</element>
      <element>mockup-patterns-livesearch</element>
      <element>pat-registry</element>
    </value>
  </records>

</registry>

Installation

We have all the files necessary to run the pattern now.

  1. Start up your Plone instance

  2. Install the Exercise 8 add-on

Running

At this point, we have no compiled version of the code that we are running with because of that our code does nothing.

  1. Go into Site Setup ‣ Resource Registries

  2. Check Development Mode

  3. Select to develop JavaScript and CSS for the exercise8 bundle

  4. Click Save

Now, you should see the livesearch pattern default to searching the current section.

Production

To build our bundle, we will utilize the plone-compile-resources script that ships with Plone.

Warning

If you are not running a ZEO setup, you will need to shut down your Plone instance since the ZODB in this mode does not allow multiple processes to access it at the same time.

An example command will look like this

./bin/plone-compile-resources --site-id=Plone --bundle=exercise8

Once this command finishes, your bundle is built and will be deployed with your package.