0

I am trying to pass function(with parameter) to another function inside the json object. It always undefined and I couldn't find the reason.

First triggering packtNs.graduateForm.loadEvent function. Then sending packtNs.common.populateWithTodaysDate("packt_supervisor", "packt_postgraduatestartdate") function to packtNs.common.wireOnChangeEvents function.

Here is my code:

var packtNs = packtNs || {};

packtNs.common = packtNs.common || {};

/**
* A method that populates the post graduate start date
* when the supervisor lookup is populated.
* @returns {Void}
*/

packtNs.common.populateWithTodaysDate = function(attributeToMonitor, dateAttributeToChange)
{
    console.log("Populatewithtodaysdate function triggered");
}

packtNs.common.wireOnChangeEvents =
    function(eventAttributeTuples){
        debugger;
        for (var i in eventAttributeTuples) {
           console.log(eventAttributeTuples[i].attribute);
           ////attribute is OK
           console.log(eventAttributeTuples[i].function);
           ////function is always undefined. eventAttributeTuples[0] object doesnt have function - tried different names
        }

}

packtNs.graduateForm = packtNs.graduateForm || {};

packtNs.graduateForm.loadEvent = function(){
    packtNs.common.wireOnChangeEvents([
        { 
            attribute: "packt_supervisor",
            function:packtNs.common.populateWithTodaysDate("packt_supervisor", "packt_postgraduatestartdate")
        }
    ]);
    debugger;
}
3
  • 3
    "inside the json object." - JSON is a string representation of data, it does not "contain" functions Commented Jun 1, 2019 at 22:22
  • 3
    function is a reserved word and should not be used as a property name. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 1, 2019 at 22:23
  • Tried different names. Same result Commented Jun 1, 2019 at 22:30

2 Answers 2

1

This is a name spaced object not JSON, but to get something you have to return something. I renamed your "function" to "mything" because well it was wrong.

var packtNs = packtNs || {};

packtNs.common = packtNs.common || {};

/**
 * A method that populates the post graduate start date
 * when the supervisor lookup is populated.
 * @returns {Void}
 */

packtNs.common.populateWithTodaysDate = function(attributeToMonitor, dateAttributeToChange) {
  console.log("Populatewithtodaysdate function triggered");
  return {
    arg1: attributeToMonitor,
    arg2: dateAttributeToChange
  };
}

packtNs.common
  .wireOnChangeEvents = function(eventAttributeTuples) {
    //debugger;
    console.log("tup:", eventAttributeTuples);
    for (var i in eventAttributeTuples) {
      console.log(eventAttributeTuples[i].attribute);
      ////attribute is OK
      console.log(eventAttributeTuples[i].mything);
      ////function is always undefined. eventAttributeTuples[0] object doesnt have function - tried different names
    }
    return eventAttributeTuples;
  }

packtNs.graduateForm = packtNs.graduateForm || {};

packtNs.graduateForm.loadEvent = function() {
  return packtNs.common.wireOnChangeEvents([{
    attribute: "packt_supervisor",
    mything: packtNs.common.populateWithTodaysDate("packt_supervisor", "packt_postgraduatestartdate")
  }]);
  // debugger;
}

var myresult = packtNs.graduateForm.loadEvent();
console.log("myresult:",myresult);

Sign up to request clarification or add additional context in comments.

Comments

1

First thing, as pointed out in comments under your question, change attribute name from "function" to something different. Then I'll recommend you to try call passed string representation of function into eval method (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.