Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Latest commit

 

History

History
52 lines (41 loc) · 1.14 KB

creating-a-custom-query.md

File metadata and controls

52 lines (41 loc) · 1.14 KB

Creating a custom query

In some cases you will need to create your own data-query method so you could reuse code logic. Let's build a simple example.

blocks.queries.formatPrice = {

  // the value is passed when calling the formatPrice
  update: function (value) {
    // this points to the HTML element
    if (value != null) {
      this.innerHTML = 'formated value: ' + value.toString();
    } else {
      this.innerHTML = '';
    }
  }  
};

And this is how you could use your custom query:

<span data-query="formatPrice(price)"></span>

Building custom queries with performance in mind

blocks.queries.formatPrice = {

  preprocess: function (value) {
    // this points to the blocks.VirtualElement instance

    if (value != null) {
      this.html('formated value: ' + value.toString());
    } else {
      this.html('');
    }
  },

  // the value is passed when calling the formatPrice
  update: function (value) {
    // this points to the HTML element

    if (value != null) {
      this.innerHTML = 'formated value: ' + value.toString();
    } else {
      this.innerHTML = '';
    }
  }  
};