Search code examples
javascriptember.jshandlebars.jsember-cli

makeBoundHelper alternative in Ember 2.0


I've been injecting Google DFP ads in my blog-posts by using a bound helper so far. Since all Handlebars APIs have been removed in Ember 2.0 what can I use as of Ember 2.0 instead?

import Ember from "ember";

export default Ember.Handlebars.makeBoundHelper(function(value, options) {

  var parsedHtml = Ember.$('<div />').html(value)

    // Push the ads after the divs have been rendered
    Ember.run.schedule('afterRender', function() {
      googletag.cmd.push(function() { googletag.display('div-gpt-ad-111111111-0'); });
    }) 
  }

  return parsedHtml.html()
});

Solution

  • You would use the Ember.Helper.helper syntax:

    import Ember from 'ember';
    
    const { Helper: { helper }, run: { schedule }, $ } = Ember;
    
    export function helperName(params, hash) {
      let parsedHtml = $('<div />').html(params[0])
    
        // Push the ads after the divs have been rendered
       schedule('afterRender', function() {
          googletag.cmd.push(function() { googletag.display('div-gpt-ad-111111111-0'); });
        }) 
      }
    
      return parsedHtml.html();
    }
    
    export default helper(helperName);
    

    Params is an array of all the values that you pass to the helper in your template such as {{my-helper val1 val2 val3}} params[0] is val1 and so on, the hash is an object containing all the properties you set on the helper {{my-helper val1 val2 property1=myPropValue}} and you would access it via hash.property1.