本文介绍了在Ember 2.0中makeBoundHelper替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过使用绑定的帮助者在我的博文中注入了Google DFP广告。由于所有Handlebars API已经在Ember 2.0中被删除,所以我可以使用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()
});


推荐答案

您将使用语法:

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是您传递给模板中的帮助器的所有值的数组,例如 {{my-helper val1 val2 val3}} params [0] val1 等等,哈希是包含您在帮助器上设置的所有属性的对象 {{my-helper val1 val2 property1 = myPropValue}} ,您将访问它通过 hash.property1

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.

这篇关于在Ember 2.0中makeBoundHelper替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:43