Skip to content

Instantly share code, notes, and snippets.

@frenchbread
Created August 10, 2015 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchbread/216d6cdc6f9f0ce95689 to your computer and use it in GitHub Desktop.
Save frenchbread/216d6cdc6f9f0ce95689 to your computer and use it in GitHub Desktop.
Peace of art actually lol :)
'submit #apiConfigurationUploadForm': function (event, template) {
// calling method that converts YAML to JSON and returns Json object
Meteor.call("convertYamlToJson", template.reactiveFile.get().name, function (err, jsonFile) {
// logging an error if one exists
if (err) console.log(err);
// iterating through JSON object keys
for (var key in jsonFile) {
if (jsonFile.hasOwnProperty(key)) {
// some fields in apiBackend schema not just strings, but arrays and objects, that is why there is a need tp
// manually populate these fields
// checking if key field contains object or an array, iterating through it manually as well as populating data
switch (key){
case "backend_protocol":
// backend_protocol is a select box, so there is a need to manually force it to select specific value
$("select[data-schema-key='"+key+"'] option[value='"+jsonFile[key]+"']").attr('selected', 'selected');
break;
case "url_matches":
// "url_matches" is an array object, so iterating through it
for(var i = 0; i<jsonFile[key].length; i++){
// updating filed value by data-schema-key attribute and its position in the array
$("input[data-schema-key='matching."+i+".frontend_prefix']").val(jsonFile[key][i].frontend_prefix);
// updating filed value by data-schema-key attribute and its position in the array
$("input[data-schema-key='matching."+i+".backend_prefix']").val(jsonFile[key][i].backend_prefix);
}
break;
case "servers":
// "servers" is an array object, so iterating through it
for(var i = 0; i<jsonFile[key].length; i++){
// updating filed value by data-schema-key attribute and its position in the array
$("input[data-schema-key='server."+i+".backend_host']").val(jsonFile[key][i].host);
// updating filed value by data-schema-key attribute and its position in the array
$("input[data-schema-key='server."+i+".backend_port']").val(jsonFile[key][i].port);
}
break;
default :
// almost all other fields are regular text fields (strings)
// updating field value by just providing the field name
$("input[data-schema-key='"+key+"']").val(jsonFile[key]);
break;
}
}
}
});
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment