Storing multiple parameters instead of one

// Populate a select list with pt codes.
    this.populateCodeList = function (ptName_, listSelector_) {

        if (!ptName_) { return self.displayError("Invalid pt name", "populatecodeList"); }
        if (!listSelector_) { return self.displayError("Invalid list selector", "populatecodeList"); }

        var pt = self.registry.pt[ptName_];
         
        if (!pt) { return self.displayError("Invalid PT: " + ptName_, "populateCodeList"); }

        var html = "";

        $_.each(pt.codes, function (index_, code) {

            if (!code || !code.id || !code.name) { return self.displayError("Invalid code at index " + index_, "populateCodeList"); }

            html += "<option value='" + code.id + "'>" + code.name + "</option>";
        });

        $_(listSelector_).html(html);
    };

I am calling the above function like the following in my code:

app_.populateCodeList("First Attribute", self.selectors.add_attribute_dialog + " .attribute-name");

Everything works fine. However, when I try to call the above function like this with second attribute separated
by comma as follows :

 app_.populateCodeList("First Attribute,Second Attribute", self.selectors.add_attribute_dialog + " .attribute-name");

I get the following error Invalid PT:First Attribute,Second Attribute in populateCodeList which is the result of this line in the above code if (!pt) { return self.displayError("Invalid PT: " + ptName_, "populateCodeList"); }

So basically my code is not programmed to handle two parameters separated by comma but single variable which is shown in the line

var pt = self.registry.pt[ptName_];

How can I make sure the above line accepts single as well as double parameters whenever needed?

Thanks

Could you check for multiple parameters, and have it go through the function for each one?

this.populateCodeList = function populateCodeList(ptName_, listSelector_) {
    if (ptName.indexOf(",") > -1) {
        return ptName_.split(",").forEach(function (ptNamePart) {
            populateCodeList(ptNamePart, listSelector_);
        });
    };
    ... // rest of the function here
};

Thanks. I did the following .However, I am getting an error in the browser console :

Uncaught ReferenceError: populateCodeList is not defined on the following line :

populateCodeList(ptNamePart, listSelector_);

// Populate a select list with pt codes.
    this.populateCodeList = function (ptName_, listSelector_) {

           if (ptName.indexOf(",") > -1) {
                return ptName_.split(",").forEach(function (ptNamePart) {
                populateCodeList(ptNamePart, listSelector_);
           });
          };

        if (!ptName_) { return self.displayError("Invalid pt name", "populatecodeList"); }
        if (!listSelector_) { return self.displayError("Invalid list selector", "populatecodeList"); }

        var pt = self.registry.pt[ptName_];
         
        if (!pt) { return self.displayError("Invalid PT: " + ptName_, "populateCodeList"); }

        var html = "";

        $_.each(pt.codes, function (index_, code) {

            if (!code || !code.id || !code.name) { return self.displayError("Invalid code at index " + index_, "populateCodeList"); }

            html += "<option value='" + code.id + "'>" + code.name + "</option>";
        });

        $_(listSelector_).html(html);
    };

Do you see the red-coloured function name in my example? It seems that your code is missing that function name.
It is that function name that helps the function to call itself with the different parameters.

I see that. Sorry about that. I was missing that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.