0

I'm working with the form builder Alpaca, and I would like to interpret a javascript function within a json datasource file, to select a certain file :

/data/options.json :

    "nature":{
        "type": "select",
        "dataSource": "function(e) {...}"
    },

This file is loaded here :

/test.html :

            $("#div").alpaca({
                "optionsSource": "/data/options.json",
                 etc...

Is this possible ?

Thanks.

jidsg
  • 11
  • 3

3 Answers3

0

You could make the function call first, then add the result to you JSON with data.nature.push();

See this answer : Add data to JSON in JS

script type="text/javascript">
var JSON = {"nature":{
    "type": "select"
}};
JSON.dataSource.push(function());

$("#form1").alpaca(JSON);
</script>
Community
  • 1
  • 1
guiguiblitz
  • 407
  • 4
  • 10
  • Well, the problem is that Alpaca is just loading the json file, but the javascript function isn't interpreted ; so I can't really make a data.nature.push() – jidsg Apr 27 '16 at 09:30
  • modify the JSON before calling your alpaca – guiguiblitz Apr 27 '16 at 09:32
  • ok. but I can't do that because the parameters of the function that I'd like to use are only avalaible once Alpaca is loaded – jidsg Apr 27 '16 at 09:35
  • well, i dont think you have the good aproach then. You need some kind of controller that modifies your alpaca as it is fild up. – guiguiblitz Apr 27 '16 at 09:37
0

You can use eval as said :

object = {"nature":{
    "type": "select",
    "dataSource": "function(e) {alert('ok')}"
    }
}

var x = eval("("+object.nature.dataSource+")");
x()

Demo

AshBringer
  • 2,614
  • 2
  • 20
  • 42
0

As the Alpace datasources doc, http://www.alpacajs.org/docs/api/datasources.html you can use custom function in the datasource parameter :

$("#field5").alpaca({
"schema": {
    "type": "string",
    "title": "Pick an Action Hero"
},
"options": {
    "type": "select",
    "dataSource": function(callback) {
        callback([{
            "value": "rambo",
            "text": "John Rambo"
        }, {
            "value": "norris",
            "text": "Chuck Norris"
        }, {
            "value": "arnold",
            "text": "Arnold Schwarzenegger"
        }]);
    }
}

});

Théo Bouveret
  • 209
  • 2
  • 9