Class: Bowline::Binders::Base

Inherits:
Object
  • Object
show all
Extended by:
Async, Desktop::Bridge::ClassMethods, Watcher::Base
Includes:
Async
Defined in:
lib/bowline/binders.rb

Direct Known Subclasses

Collection, Singleton

Defined Under Namespace

Modules: Async

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Watcher::Base

extended, included, watcher

Methods included from Desktop::Bridge::ClassMethods

js_expose

Constructor Details

#initialize(id, *args) ⇒ Base

:nodoc:



244
245
246
# File 'lib/bowline/binders.rb', line 244

def initialize(id, *args) #:nodoc:
  @item = self.class.find(id)
end

Instance Attribute Details

#itemObject (readonly)

Instance of the bound class’ record



242
243
244
# File 'lib/bowline/binders.rb', line 242

def item
  @item
end

Class Method Details

.bowlineObject

JavaScript proxy to the Bowline object. See Bowline::Desktop::Proxy for more information. Example:

bowline.log("msg").call


223
224
225
# File 'lib/bowline/binders.rb', line 223

def bowline
  page.Bowline
end

.callback(result = nil) ⇒ Object



110
111
112
113
# File 'lib/bowline/binders.rb', line 110

def callback(result = nil)
  result = yield if block_given?
  callback_proc.call(result)
end

.callback_proc(proc = nil) ⇒ Object

:nodoc:



104
105
106
107
# File 'lib/bowline/binders.rb', line 104

def callback_proc(proc = nil) #:nodoc:
  Thread.current[:callback] = proc if proc
  Thread.current[:callback]
end

.callback_proc=Object

:nodoc:



108
109
110
111
# File 'lib/bowline/binders.rb', line 108

def callback_proc(proc = nil) #:nodoc:
  Thread.current[:callback] = proc if proc
  Thread.current[:callback]
end

.created(item) ⇒ Object

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.



175
176
177
178
179
180
181
# File 'lib/bowline/binders.rb', line 175

def created(item)
  bowline.created(
    name, 
    item.id, 
    item.to_js
  ).call
end

.destroyed(item) ⇒ Object

Remove an item from the binder, updating the HTML. This method is normally only called internally by the bound class’s after_destroy callback.



197
198
199
200
201
202
# File 'lib/bowline/binders.rb', line 197

def destroyed(item)
  bowline.destroyed(
    name, 
    item.id
  ).call
end

.find(id) ⇒ Object

Calls .find on the klass sent to the bind method. This is used internally, to find records when the page invoke instance methods.



163
164
165
# File 'lib/bowline/binders.rb', line 163

def find(id)
  klass.find(id)
end

.initialObject

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


157
158
# File 'lib/bowline/binders.rb', line 157

def initial
end

.instance_invoke(id, method, *args) ⇒ Object

:nodoc:



124
125
126
# File 'lib/bowline/binders.rb', line 124

def instance_invoke(id, method, *args) #:nodoc:
  self.new(id).send(method, *args)
end

.items=(items) ⇒ Object

Set the binder’s items. This will replace all items, and update the HTML.



168
169
170
# File 'lib/bowline/binders.rb', line 168

def items=(items)
  bowline.replace(name, items.to_js).call
end

.jqueryObject

Javascript proxy to jQuery. See Bowline::Desktop::Proxy for more information. Example:

jquery.getJSON("http://example.com").call


231
232
233
# File 'lib/bowline/binders.rb', line 231

def jquery
  page.jQuery
end

.js_invoke(window, callback, method, *args) ⇒ Object

:nodoc:



115
116
117
118
119
120
121
122
# File 'lib/bowline/binders.rb', line 115

def js_invoke(window, callback, method, *args) #:nodoc:
  self.callback_proc = callback
  if method == :setup
    setup(window)
  else
    send(method, *args)
  end
end

.klassObject

Returns class set by the ‘bind’ method



205
206
207
# File 'lib/bowline/binders.rb', line 205

def klass
  @klass || raise("klass not set - see bind method")
end

.loggerObject

See Bowline::logger



236
237
238
# File 'lib/bowline/binders.rb', line 236

def logger
  Bowline::logger
end

.pageObject

JavaScript proxy to the page. See Bowline::Desktop::Proxy for more information. Example:

page.myFunc(1,2,3).call


213
214
215
216
217
# File 'lib/bowline/binders.rb', line 213

def page
  Bowline::Desktop::Proxy.new(
    windows.length == 1 ? windows.first : windows
  )
end

.populate(items = initial) ⇒ Object

Populate initial items



146
147
148
# File 'lib/bowline/binders.rb', line 146

def populate(items = initial)
  self.items = items if items
end

.setup(window) ⇒ Object

:nodoc:



138
139
140
141
142
143
# File 'lib/bowline/binders.rb', line 138

def setup(window) #:nodoc:
  Binders.active(self)
  windows(window)
  populate
  callback(true)
end

.updated(item) ⇒ Object

Update an item on the binder, updating the HTML. This method is normally only called internally by the bound class’s after_update callback.



186
187
188
189
190
191
192
# File 'lib/bowline/binders.rb', line 186

def updated(item)
  bowline.updated(
    name, 
    item.id, 
    item.to_js
  ).call
end

.windows(window = nil) ⇒ Object

An array of window currently bound.



129
130
131
132
133
134
135
136
# File 'lib/bowline/binders.rb', line 129

def windows(window = nil)
  @windows ||= []
  if window
    @windows << window
    @windows.uniq!
  end
  @windows
end