Created
December 27, 2011 06:41
-
-
Save jeffreyiacono/1522893 to your computer and use it in GitHub Desktop.
module that generates json for usage by sencha touch 2 nested list menu component
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
module Sencha | |
module NestedListMenu | |
class << self | |
# Sencha Touch nested list menu requires json in the form of: | |
# | |
# {"text": "some identifier", | |
# "children": ["text": "child identifier", "other": "info", "leaf": "true"], | |
# [ ... ] | |
# }, | |
# { ... } | |
# | |
# Note the "leaf" : "true" within the children element. This is important to indicate | |
# this is the leaf of the nested tree menu for Sencha. | |
# | |
# You can override "text" and "children" to be whatever you want. | |
# | |
# The method takes a set of records as a param and converts the parent / children records into the desired | |
# syntax. Here, the records are simply named parents and children, change as needed. | |
def nested_list_json(parents) | |
parents.reduce([]) do |agg, parent| | |
agg << { text: parent.text, | |
id: parent.id, | |
children: parent.children.map { |c| {text: c.text, id: c.id, leaf: true }} | |
} | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Jeffrey, could you please explain a bit on how to run this against an existing json doc to generate sencha-nestedlist-friendly json? I've been struggling a bit to get a large json doc into this leaf-format. Thank you!