问题
I have a helper function (ifCond) in ember.js as below
export default () => {
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
alert("hi");
if (params[3]) { //handle case insensitive conditions if 4 param is passed.
params[0] = params[0].toLowerCase();
params[2] = params[2].toLowerCase();
}
let v1 = params[0];
let operator = params[1];
let v2 = params[2];
switch (operator) {
case '==':
return (v1 == v2);
case '!=':
return (v1 != v2);
case '===':
return (v1 === v2);
case '<':
return (v1 < v2);
case '<=':
return (v1 <= v2);
case '>':
return (v1 > v2);
case '>=':
return (v1 >= v2);
case '&&':
return !!(v1 && v2);
case '||':
return !!(v1 || v2);
default:
return false;
}
});
}
when I am trying to access this function in my hbs file as below
{{#if (ifCond novv.ViolationId '==' noviv.ViolationId true)}}
{{log 'someVariable'}}
<br />
{{/if}}
I am getting error as below:
Any help please - thanks in advance
回答1:
From your example it looks like you may be working with a very old version of Ember. The first place to start is with the Ember Guide on writing helpers. There is a version selector in the top right corner of each guide that will take you back to previous documentation. A guess form your example is that you're working with something around 1.11.
I would highly recommend that you update to a newer version of ember and use ember-cli as that will make the import and discovery process for helpers much smoother along with gaining access to newer features and security updates.
回答2:
Did it my friend, its done. Here it is, the previous developer put all the helper functions in one file, the confusion he created was, he put other files with those helper functions in different directories - it seems I need to clean that up. And your suggestion to check the define helped me to look into the webpack.config.js (async helpers, async __parts), there he is combining all the files scripts and generating as one js file which is written on to app.js. The convention he (or Ember I am not sure) followed was camel casing. For example for one helper-function he written as below withing the same ifCond.js file, is converted as "replace", we can use it as replace in handlebar
IMS.ReplaceHelper = Ember.Helper.extend({
compute(args) {
return args[0].replace(new RegExp(args[1], 'ig'), args[2]);
}
})
If suppose, if there are two Words - then it follows the camel casing (you know it). Totally completed and I have written some helper functions of my own as needed - thanks a lot for everybody who jumped-in to help me - thanks a lot and I don't have words how to describe how happy I am - thank you. My own helper functions as below:
IMS.IsLastHelper = Ember.Helper.extend({
compute(args) {
var list = args[0];
var item = args[1];
if (Array.isArray(list)) {
var id = list.indexOf(item);
return id == list.length - 1;
}
return false;
}
})
IMS.IsFirstHelper = Ember.Helper.extend({
compute(args) {
var list = args[0];
var item = args[1];
if (Array.isArray(list)) {
var id = list.indexOf(item);
return id == 0;
}
return false;
}
})
And I called them with syntax as below:
{{#each model.novs as |nov index|}}
{{#if (isFirst model.novs nov)}}
({{nov.NOVNumber}}:
{{else}}
{{nov.NOVNumber}}:
{{/if}}
{{#each nov.Violations as |novv index|}}
{{#unless (isLast nov.Violations novv)}}
{{novv.ViolationNumber}},
{{else}}
{{#if (isLast model.novs nov)}}
{{novv.ViolationNumber}}
{{else}}
{{novv.ViolationNumber}};
{{/if}}
{{/unless}}
{{/each}}
{{#if (isLast model.novs nov)}}
)
{{/if}}
{{/each}}
Thanks again to everybody in this group. :pray:
来源:https://stackoverflow.com/questions/58722212/i-am-gettting-the-error-compile-error-ifcond-is-not-a-helper-when-i-am-trying