lib/bowline/binders.rb in bowline-0.5.8 vs lib/bowline/binders.rb in bowline-0.6.0

- old
+ new

@@ -63,26 +63,27 @@ class << self # An array of window currently bound. def windows @windows ||= [] end - + def setup(window) #:nodoc: self.windows << window - items = initial - items.try(:to_js) || [] + if initial_items = initial + self.items = initial_items + end + Hash.new # Empty options 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. # Example: # def initial # klass.all(:limit => 10) # end def initial - klass.all end def js_invoke(window, method, *args) #:nodoc: if method == :setup setup(window) @@ -102,11 +103,11 @@ klass.find(id) end # Set the binder's items. This will replace all items, and update the HTML. def items=(items) - bowline.populate(name, items.to_js).call + bowline.replace(name, items.to_js).call end # Add a new item to the binder, updating the HTML. # This method is normally only called internally by # the bound class's after_create callback. @@ -136,106 +137,74 @@ bowline.removed( name, item.id ).call end - - protected - # Associate the binder with a model to setup callbacks so - # changes to the model are automatically reflected in the view. - # Example: - # bind Post - # - # When the bound class is created/updated/deleted - # the binder's callbacks are executed and the view - # updated accordingly. - # - # Classes inheriting fromActiveRecord and Bowline::LocalModel are - # automatically compatable, but if you're using your own custom model - # you need to make sure it responds to the following methods: - # * all - return all records - # * find(id) - find record by id - # * after_create(method) - after_create callback - # * after_update(method) - after_update callback - # * after_destroy(method) - after_destroy callback - # - # The klass' instance needs to respond to: - # * id - returns record id - # * to_js - return record's attribute hash - # - # You can override the to_js method on the model instance - # in order to return specific attributes for the view. - def bind(klass) - @klass = klass - @klass.after_create(method(:created)) - @klass.after_update(method(:updated)) - @klass.after_destroy(method(:removed)) - end - - # Returns class set by the 'bind' method - def klass - @klass || raise("klass not set - see bind method") - end - - # JavaScript proxy to the page. - # See Bowline::Desktop::Proxy for more information. - # Example: - # page.myFunc(1,2,3).call - def page - Bowline::Desktop::Proxy.new( - windows.length == 1 ? windows.first : windows - ) - end - - # JavaScript proxy to the Bowline object. - # See Bowline::Desktop::Proxy for more information. - # Example: - # bowline.log("msg").call - def bowline - page.Bowline - end + + # Returns class set by the 'bind' method + def klass + @klass || raise("klass not set - see bind method") + end - # Javascript proxy to jQuery. - # See Bowline::Desktop::Proxy for more information. - # Example: - # jquery.getJSON("http://example.com").call - def jquery - page.jQuery - 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 + # JavaScript proxy to the page. + # See Bowline::Desktop::Proxy for more information. + # Example: + # page.myFunc(1,2,3).call + def page + Bowline::Desktop::Proxy.new( + windows.length == 1 ? windows.first : windows + ) + end + + # JavaScript proxy to the Bowline object. + # See Bowline::Desktop::Proxy for more information. + # Example: + # bowline.log("msg").call + def bowline + page.Bowline + end + + # Javascript proxy to jQuery. + # See Bowline::Desktop::Proxy for more information. + # Example: + # jquery.getJSON("http://example.com").call + def jquery + page.jQuery + 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 # jQuery element object attr_reader :element \ No newline at end of file