lib/bowline/binders.rb in bowline-0.6.2 vs lib/bowline/binders.rb in bowline-0.6.3

- old
+ new

@@ -58,22 +58,76 @@ class Base extend Bowline::Watcher::Base extend Bowline::Desktop::Bridge::ClassMethods js_expose - class << self + module Async + protected + def async(*methods) + if block_given? + Thread.new(callback_proc) do |proc| + begin + self.callback_proc = proc + yield + rescue => e + Bowline::Logging.log_error(e) + end + end + else + methods.each do |method| + class_eval(<<-EOS, __FILE__, __LINE__) + def #{method}_with_async(*args, &block) + async { #{method}_without_async(*args, &block) } + end + EOS + alias_method_chain method, :async + end + end + end + end + + extend Async + include Async + class << self; extend Async; end + + class << self + def callback_proc(proc = nil) #:nodoc: + Thread.current[:callback] = proc if proc + Thread.current[:callback] + end + alias_method :callback_proc=, :callback_proc + + def callback(result = nil) + result = yield if block_given? + callback_proc.call(result) + end + + def js_invoke(window, callback, method, *args) #:nodoc: + self.callback_proc = callback + if method == :setup + setup(window) + else + send(method, *args) + end + end + + def instance_invoke(id, method, *args) #:nodoc: + self.new(id).send(method, *args) + end + # An array of window currently bound. def windows @windows ||= [] end def setup(window) #:nodoc: self.windows << window + self.windows.uniq! if initial_items = initial self.items = initial_items end - Hash.new # Empty options + callback(true) end # Called by a window's JavaScript whenever that window is bound to this Binder. # This method populates the window's HTML with all bound class' records. # Override this if you don't want to send all the class' records to the window. @@ -82,22 +136,10 @@ # klass.all(:limit => 10) # end def initial end - def js_invoke(window, method, *args) #:nodoc: - if method == :setup - setup(window) - else - send(method, *args) - end - end - - def instance_invoke(id, meth, *args) #:nodoc: - self.new(id).send(meth, *args) - end - # Calls .find on the klass sent to the bind method. # This is used internally, to find records when the page # invoke instance methods. def find(id) klass.find(id) @@ -131,12 +173,12 @@ end # Remove an item from the binder, updating the HTML. # This method is normally only called internally by # the bound class's after_destroy callback. - def removed(item) - bowline.removed( + def destroyed(item) + bowline.destroyed( name, item.id ).call end @@ -172,65 +214,29 @@ end # See Bowline::logger def logger Bowline::logger - end - - # Trigger events on all elements - # bound to this binder. - # Example: - # trigger(:reload, {:key => :value}) - def trigger(event, data = nil) - bowline.trigger( - name, - format_event(event), - data - ).call - end - - # Helper method to trigger a loading - # event either side of a block: - # loading { - # # Slow code, e.g. http call - # } - def loading(&block) - trigger(:loading, true) - yield - trigger(:loading, false) - end - - def format_event(name) #:nodoc: - name.is_a?(Array) ? - name.join('.') : - name.to_s - end + end end - - # jQuery element object - attr_reader :element - + # Instance of the bound class' record attr_reader :item def initialize(id, *args) #:nodoc: - @element = self.class.bowline.element( - self.class.name, id - ) - @item = self.class.find(id) + @item = self.class.find(id) end protected - # Trigger jQuery events on this element. - # Example: - # trigger(:highlight) - def trigger(event, data = nil) - element.trigger( - self.class.format_event(event), - data - ).call + def callback_proc(*args) #:nodoc + self.class.callback_proc(*args) end - + alias_method :callback_proc=, :callback_proc + + def callback(*args, &block) + self.class.callback(*args, &block) + end + # Remove element from the view def remove! self.class.removed(item) end \ No newline at end of file