Garbonzo_Horowitz
asked on
Re-writing a JavaScript ternary block
How do I rewrite a JavaScript ternary line inside this code:
// Change the target user
const typeaheadOptions = {
selector: '#user',
name: 'userTypeahead',
display: 'NAME',
ajaxURL: '/index.cfm?',
qryParams: [{
name: 'fuseaction',
value: 'main.getJSON',
prefetch: true,
remote: true,
}, {
name: 'isJSON',
value: true,
prefetch: true,
remote: true,
}, {
name: 'qName',
value: 'getUsers',
prefetch: true,
remote: true,
}, {
name: 'name',
value: '%QUERY',
prefetch: false,
remote: true,
}],
wildCardParam: '%QUERY',
id: 'NAMECODE',
limit: 150,
templates: {
suggestion: "<div>{{NAME}}</div>"
},
events: [{
eventType: 'select',
callback: function(ev, suggestion) {
iptws.unsubscribe('iptdeadlines.' + caseModel.activeItem.NAMECODE());
caseModel.activeItem.NAMECODE(suggestion.NAMECODE);
iptws.subscribe('iptdeadlines.' + caseModel.activeItem.NAMECODE());
const params = {
fuseaction: 'main.getJSON',
isJSON: true,
qName: (caseModel.currentData() === 'daily') ? 'getUserDeadlines' : 'getWeeklyDeadlines',
namecode: suggestion.NAMECODE,
};
$.when(
$.ajax({
method: 'GET',
url: '/index.cfm',
data: params
}),
...
I tried adding this before the line "// Change target user" :
let whatQuery;
if(caseModel.currentData() === 'daily'){
whatQuery = 'getUserDeadlines'
} else if (caseModel.currentData() === 'weekly') {
whatQuery = 'getWeeklyDeadlines'
} else {
whatQuery = 'getCustomDeadlines'
}
Then I replaced the bolded ternary code with this line:
qName: whatQuery,
But that did not work. JS complained about the '{' character here:
=== 'daily'){
So it looks like changing the ternary line with if statements is not working at that location. How can I change the ternary code and use if statements instead?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I missed one of your ifs. Here is a simpler version using an object
let deadlines = {'daily':'getUserDeadlines', 'weekly' : 'getWeeklyDeadlines'}[caseModel.currentData()];
if (!deadlines) deadlines = 'getCustomDeadlines' // default
ASKER
Thank you Michel and Scott. Both solutions are good. I went with the modified ternary because it's similar to what I previously had in there.
I did not realise you WANTED a ternary at all. I expected you wanted to get rid of it. Anyway
Open in new window