motion-prime/sections/form.rb in motion-prime-0.9.6 vs motion-prime/sections/form.rb in motion-prime-0.9.7
- old
+ new
@@ -16,22 +16,22 @@
# puts "Submitted email: #{email}"
# end
# end
#
- class_attribute :fields_options, :text_field_limits, :text_view_limits
+ class_attribute :fields_options, :text_field_limits, :text_view_limits, :fields_callbacks
attr_accessor :fields, :field_indexes, :keyboard_visible, :rendered_views, :grouped_data
def table_data
if has_many_sections?
- grouped_data.compact
+ grouped_data.reject(&:nil?)
else
fields.values
end
end
- def reload_cell_section(section)
+ def hard_reload_cell_section(section)
field = section.name.to_sym
path = field_indexes[field]
section.cell.try(:removeFromSuperview)
fields[field] = load_field(self.class.fields_options[field])
@@ -41,14 +41,11 @@
else
@data[path.section][path.row] = fields[field]
end
set_data_stamp(fields[field].object_id)
-
- # table_view.beginUpdates
table_view.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimationNone)
- # table_view.endUpdates
end
# Returns element based on field name and element name
#
# Examples:
@@ -171,11 +168,11 @@
end
def reload_data
@groups_count = nil
init_form_fields # must be before resetting to reflect changes on @data
- reset_data
+ reset_table_data
reload_table_data
end
def has_many_sections?
group_header_options.present? || grouped_data.count > 1
@@ -189,24 +186,32 @@
def on_cell_render(cell, index)
options = data[index.row].try(:options)
if options && options[:after_render]
self.send(options[:after_render])
end
+
+ section = cell_section_by_index(index)
+ if callbacks = fields_callbacks.try(:[], section.name)
+ callbacks.each do |options|
+ options[:method].to_proc.call(options[:target] || self)
+ end
+ end
super
end
# Table View Delegate
# ---------------------
def number_of_groups(table = nil)
- has_many_sections? ? grouped_data.compact.count : 1
+ has_many_sections? ? grouped_data.reject(&:nil?).count : 1
end
class << self
def inherited(subclass)
super
subclass.fields_options = self.fields_options.try(:clone)
+ subclass.fields_callbacks = self.fields_callbacks.try(:clone)
subclass.text_field_limits = self.text_field_limits.try(:clone)
subclass.text_view_limits = self.text_view_limits.try(:clone)
end
def async_table_data(options = {})
@@ -221,10 +226,17 @@
self.fields_options ||= {}
self.fields_options[name] = options
self.fields_options[name]
end
+ def after_field_render(field_name, method, options = {})
+ options.merge!(method: method)
+ self.fields_callbacks ||= {}
+ self.fields_callbacks[field_name] ||= []
+ self.fields_callbacks[field_name] << options
+ end
+
def limit_text_field_length(name, limit)
self.text_field_limits ||= {}
self.text_field_limits[name] = limit
end
def limit_text_view_length(name, limit)
@@ -250,9 +262,12 @@
grouped_data[section_id] ||= []
section_indexes[section_id] ||= 0
section = load_field(field)
+ if section.options[:after_render].present?
+ puts "DEPRECATION: form field's option :after_render is deprecated, please use Prime::Section#after_field_render instead"
+ end
self.fields[key] = section
self.field_indexes[key] = NSIndexPath.indexPathForRow(section_indexes[section_id], inSection: section_id)
grouped_data[section_id][section_indexes[section_id]] = section
section_indexes[section_id] += 1
\ No newline at end of file