javascriptrestopenidm

How to Implement function return from http request


I have an ajax call as follow

  $.ajax({
    datatype:'json',
    url: 'http://localhost:9090/openidm/policy/managed/user/'+storeUserId,
    type:'get',
    xhrFields: {
        withCredentials: true
    } ,   
    corssDomain:true,
    headers: {
        "X-Requested-With":"XMLHttpRequest" 
    }, 
    success: function (result){
        var validations = result.properties[1].policies[1];
        console.log(validations.policyFunction);
    },
    error:function (error){
        console.log (error);
    }
   });
});

The above ajax call return the policyFunction as follow :

    function (fullObject, value, params, property) {
    var isRequired = _.find(this.failedPolicyRequirements, function (fpr) {
        return fpr.policyRequirement === "REQUIRED";
    }), isNonEmptyString = (typeof (value) === "string" && value.length), hasMinLength = isNonEmptyString ? (value.length >= params.minLength) : false;
    if ((isRequired || isNonEmptyString) && !hasMinLength) {
        return [{"policyRequirement":"MIN_LENGTH", "params":{"minLength":params.minLength}}];
    }
    return [];
}

i wanna to implement that function in my javascript file. Like passing parameters to that function. So how can i implement.


Solution

  • You can invoke it in the browser like so:

    policyFunction = eval("(" + validations.policyFunction + ")");
    
    failures = policyFunction.call({ failedPolicyRequirements: [] },
        fullObject,
        value,
        params,
        propertyName);
    

    Where fullObject is the entire object, value is the value of the particular property, params are any parameters needed within the validation function, and propertyName is the name of the property.