lib/glimmer/data_binding/model_binding.rb in glimmer-2.3.0 vs lib/glimmer/data_binding/model_binding.rb in glimmer-2.4.0
- old
+ new
@@ -99,10 +99,14 @@
end
def computed_by
Concurrent::Array.new([@binding_options[:computed_by]].flatten.compact)
end
+
+ def observation_options
+ @binding_options.slice(:recursive)
+ end
def nested_property_observers_for(observer)
@nested_property_observers_collection ||= Concurrent::Hash.new
unless @nested_property_observers_collection.has_key?(observer)
@nested_property_observers_collection[observer] = nested_property_names.reduce(Concurrent::Hash.new) do |output, property_name|
@@ -119,11 +123,11 @@
end
end
@nested_property_observers_collection[observer]
end
- def add_observer(observer)
+ def add_observer(observer, extra_options = {})
if computed?
add_computed_observers(observer)
elsif nested_property?
add_nested_observers(observer)
else
@@ -131,23 +135,24 @@
converted_value = evaluate_property
observer.call(converted_value).tap do
apply_processor(@binding_options[:after_read], converted_value)
end
end
- observer_registration = model_binding_observer.observe(model, property_name)
+ observer_registration = model_binding_observer.observe(model, property_name, observation_options)
my_registration = observer.registration_for(self)
observer.add_dependent(my_registration => observer_registration)
end
end
- def remove_observer(observer)
+ def remove_observer(observer, extra_options = {})
if computed?
@computed_model_bindings.each do |computed_model_binding|
computed_observer_for(observer).unobserve(computed_model_binding)
end
@computed_observer_collection.delete(observer)
elsif nested_property?
+ # No need to call remove_nested_observers(observer) (cleanup happens automatically indirectly when invoked through observer.unobserve(model_binding))
nested_property_observers_for(observer).clear
else
observer.unobserve(model, property_name)
end
end
@@ -165,11 +170,11 @@
@computed_observer_collection[observer]
end
def add_computed_observers(observer)
@computed_model_bindings.each do |computed_model_binding|
- observer_registration = computed_observer_for(observer).observe(computed_model_binding)
+ observer_registration = computed_observer_for(observer).observe(computed_model_binding, observation_options)
my_registration = observer.registration_for(self)
observer.add_dependent(my_registration => observer_registration)
end
end
@@ -189,16 +194,16 @@
parent_observer = nested_property_observers[parent_property_name]
end
parent_property_name = nil if parent_property_name.to_s.start_with?('[')
unless model.nil?
# TODO figure out a way to deal with this more uniformly
- observer_registration = property_indexed?(property_name) ? nested_property_observer.observe(model) : nested_property_observer.observe(model, property_name)
- parent_registration = parent_observer.registration_for(parent_model, parent_property_name)
+ observer_registration = property_indexed?(property_name) ? nested_property_observer.observe(model, observation_options) : nested_property_observer.observe(model, property_name, observation_options)
+ parent_registration = parent_observer.registration_for(parent_model, *[parent_property_name].compact)
parent_observer.add_dependent(parent_registration => observer_registration)
end
end
end
-
+
def call(value, *extra_args)
return if model.nil?
converted_value = value
invoke_property_writer(model, "#{property_name}=", converted_value) unless converted_value == evaluate_property
end