Module: Bowline::Binders

Defined in:
lib/bowline/binders.rb,
lib/bowline/binders/observer.rb,
lib/bowline/binders/singleton.rb,
lib/bowline/binders/collection.rb

Defined Under Namespace

Classes: Base, Collection, Observer, Singleton

Class Method Summary collapse

Class Method Details

.active(binder = nil) ⇒ Object

Binders are a central part of Bowline. They perform two main functions:

1) Bind a model to the view, so any changes to the model get automatically
   reflected in the view.
2) View abstraction of the model. You can define view specific class & instance
   methods, and easily call them from bound JavaScript objects.

To use a binder, you first need to bind it to a model using the bind method. Example:

class UsersBinder < Bowline::Binders::Base
  bind User
end

Once a class is bound, any updates to the model automatically update any bound HTML. The class names in the HTML are tied to the model's attribute names. You can bind HTML using the bowline.js bindup function. Example:

<div id="users">
  <div class="item">
    <span class="name"></span>
  </div>
</div>
<script>
  $("#users").bindup('UsersBinder');
</script>

Class methods

You can define class methods on your binder, and call them using JavaScript using the invoke function on the bound HTML element. Example:

<script>
  var users = $("#users").bindup('UsersBinder');
  users.invoke("method_name", "arg1", "arg2")
</script>

Instance methods

You can call your binders instance method from JavaScript by calling the invoke function on the generated HTML elements. Your binder's instance methods have access to an 'element' variable, which is the jQuery element, and a 'item' variable, which is the bound model's instance record.

Example:

class UsersBinder < Bowline::Binders::Base
  bind User
  def charge!
    #...
  end
end

<script>
  $('#users').items(10).invoke('charge!');
</script>

For more documentation on Bowline's JavaScript API, see bowline.js



59
60
61
62
63
64
65
66
# File 'lib/bowline/binders.rb', line 59

def active(binder = nil) #:nodoc:
  @active ||= []
  if binder
    @active << binder
    @active.uniq!
  end
  @active
end