public
Created

module that generates json for usage by sencha touch 2 nested list menu component

  • Download Gist
sencha.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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

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!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.