Created
March 11, 2015 22:15
-
-
Save guysmoilov/de11014dab0c1a09dc68 to your computer and use it in GitHub Desktop.
Meteor template that can recursively display any document's structure.
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
<template name="fieldSpreader"> | |
{{#if isArray}} | |
{{> arraySpreader}} | |
{{else}} | |
{{#if isObject}} | |
{{> objectSpreader}} | |
{{else}} | |
{{!-- This is just a value type --}} | |
{{ this}} | |
{{/if}} | |
{{/if}} | |
</template> | |
<template name="objectSpreader"> | |
<ul> | |
{{#each keyValuePair}} | |
<li>{{key}}:{{> fieldSpreader value}}</li> | |
{{/each}} | |
</ul> | |
</template> | |
<template name="arraySpreader"> | |
<ul> | |
{{#each this}} | |
<li>{{> fieldSpreader}}</li> | |
{{/each}} | |
</ul> | |
</template> | |
{!-- This is the containing template you should include with a document context --}} | |
<template name="documentSpreader"> | |
{{> objectSpreader}} | |
</template> |
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
spreaderIsArray = function(obj) { | |
return (obj && obj.constructor && (obj.constructor === Array)); | |
} | |
spreaderIsObject = function(obj) { | |
return ((!spreaderIsArray(obj)) && obj && obj.constructor && (obj.constructor === Object)); | |
} | |
Template.fieldSpreader.helpers({ | |
isObject: function() { | |
return spreaderIsObject(this); | |
}, | |
isArray: function() { | |
return spreaderIsArray(this); | |
} | |
}); | |
Template.objectSpreader.helpers({ | |
keyValuePair: function() { | |
var pairs = []; | |
for (k in this) { | |
pairs.push({ | |
key: k, | |
value: this[k] | |
}); | |
} | |
return pairs; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment