我在我的requirejs主干应用程序中包含了handlebarjs,我想在模板上实现一个行类帮助器定义(奇数|偶数),
我正在尝试将本机车把助手“移植”到基于模块的(requirejs): http ://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers
带有用于requirejs的handlebarjs插件: https ://github.com/SlexAxton/require-handlebars-plugin
这是我到目前为止得到的一个oddevenhelper.js:
define(['handlebars'], function ( Handlebars ){
function oddevenhelper(array, even, odd, fn, elseFn) {
if (array && array.length > 0) {
var buffer = "";
for (var i = 0, j = array.length; i < j; i++) {
var item = array[i];
// we'll just put the appropriate stripe class name onto the item for now
item.stripeClass = (i % 2 == 0 ? even : odd);
// show the inside of the block
buffer += fn(item);
}
// return the finished buffer
return buffer;
} else {
return elseFn();
}
};
Handlebars.registerHelper( 'oddevenhelper', oddevenhelper );
return oddevenhelper;
});
这是我的示例模板:
<div class="orderssummary-list">
{{#oddevenhelper orderssummary "even" "odd"}}
<div class="ordersummary-list-item {{stripeClass}}">
<a class="addone"><i class="icon-chevron-up icon-white"></i></a>
<a class="minusone"><i class="icon-chevron-down icon-white"></i></a>
<div class="item-quantity">{{quantity}}x</div><div class="item-name">{{item_name}}</div> {{total}}
<div class="clearfix">
</div>
{{/oddevenhelper}}
</div>
助手运行,但车把抛出错误,我猜是我在模板上声明的第三个或第四个参数未定义或根本没有。
我错过了什么吗?或者我只是让上下文(orderssummary)被重新定义以包含行类?