Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created December 27, 2011 06:41
Show Gist options
  • Save jeffreyiacono/1522893 to your computer and use it in GitHub Desktop.
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
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
@alexanderscott
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment