Link to home
Start Free TrialLog in
Avatar of Garbonzo_Horowitz
Garbonzo_HorowitzFlag for United States of America

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
                    }),
... 

Open in new window


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' 
    }

Open in new window

Then I replaced the bolded ternary code with this line: 

                    qName: whatQuery,

Open in new window


But that did not work. JS complained about the '{' character here:

=== 'daily'){

Open in new window

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? 


Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

No sure why you do not want the ternary, but here is a suggestion

callback: function(ev, suggestion) {
    let deadlines = {'daily':'getUserDeadlines', 'weekly' : 'getWeeklyDeadlines'}[caseModel.currentData()];
    if (!deadlines) deadlines = 'getCustomDeadlines' // default
    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: deadlines,
      namecode: suggestion.NAMECODE,
    };

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

Avatar of Garbonzo_Horowitz

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