Last active
August 29, 2015 14:26
-
-
Save mdijoux/da156093804dd5ab256d to your computer and use it in GitHub Desktop.
SimpleSchema formly Mapping with walk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formlyFieldsFromSchema(sschema) { | |
var fields = []; | |
var currentGroup = fields; | |
var groupStack = []; | |
var callbacks = { | |
leaf: function (sschema, key) { | |
var schema = sschema.schema(key); | |
//SimpleSchema <-> formly mapping | |
var mapping = { | |
key: key, | |
type: 'input', | |
templateOptions: { | |
label: sschema.label(key), | |
required: !schema.optional | |
} | |
} | |
currentGroup.push(mapping); | |
}, | |
array: { | |
prefix: function (sschema, key) { }, | |
intermed: function (sschema, key, i) { }, | |
sufix: function (sschema, key) { } | |
}, | |
subSchema: { | |
prefix: function (sschema, key) { | |
if (key !== '') { | |
//Not root | |
//Create a fieldGroup | |
var fieldGroup = { | |
fieldGroup: [] | |
}; | |
groupStack.push(currentGroup); | |
currentGroup = fieldGroup.fieldGroup; | |
} | |
}, | |
intermed: function (sschema, key, i) { | |
//No-op | |
}, | |
sufix: function (sschema, key) { | |
//Close fieldGroup? | |
currentGroup = groupStack.pop(); | |
} | |
} | |
}; | |
SimpleSchema.walk(sschema, '', callbacks); | |
return fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment