Skip to content

Commit

Permalink
Plugins now support callbacks from native code.
Browse files Browse the repository at this point in the history
Expanded the alert plugin to include a simple confirm method, that
uses the callback.
  • Loading branch information
gga committed Nov 11, 2012
1 parent d3ead8c commit 731af09
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
14 changes: 11 additions & 3 deletions lib/calatrava/templates/kernel/app/calatrava.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ calatrava.inbound =
fireTimer: (timerId) ->
calatrava.bridge.timers.fireTimer(timerId)

invokeCallback: (widgetName) ->
extraArgs = _.map(_.toArray(arguments).slice(1), ((obj) -> obj.valueOf() if obj?))
calatrava.bridge.widgets.callback(widgetName).apply(this, extraArgs)
invokePluginCallback: (handle, data) ->
calatrava.bridge.plugins.invokeCallback(handle, data)

calatrava.bridge.changePage = (target) ->
calatrava.bridge.runtime.changePage(target)
Expand Down Expand Up @@ -180,6 +179,7 @@ calatrava.bridge.timers = (() ->

calatrava.bridge.plugins = (() ->
registered = {}
callbacks = {}

call: (pluginName, method, argMessage) ->
calatrava.bridge.runtime.callPlugin(pluginName, method, argMessage)
Expand All @@ -189,6 +189,14 @@ calatrava.bridge.plugins = (() ->

run: (pluginName, method, argMessage) ->
registered[pluginName](method, argMessage)

rememberCallback: (callback) ->
_.tap calatravaId(), (handle) ->
callbacks[handle] = callback

invokeCallback: (handle, data) ->
callbacks[handle](data)
delete callbacks[handle]
)()

calatrava.bridge.plugin = (name, impl) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ example.converter.controller = ({views, changePage, ajax}) ->
enabled: c != unselectableCurrency
selected: c == selectedCurrency

performConversion = (amount) ->
outRate = currencyRate[outCurrency]
inRate = currencyRate[inCurrency]
views.conversionForm.render
out_amount: (Math.round(amount * (outRate / inRate) * 100)) / 100

convert = () ->
views.conversionForm.get 'in_amount', (inAmount) ->
if inAmount == ""
calatrava.bridge.alert "Need to enter an amount to convert."

outRate = currencyRate[outCurrency]
inRate = currencyRate[inCurrency]
views.conversionForm.render
out_amount: (Math.round(inAmount * (outRate / inRate) * 100)) / 100
calatrava.confirm "No amount to convert. Convert one instead?", (convertOne) ->
performConversion(1) if convertOne
else
performConversion(inAmount)

views.conversionForm.bind 'convert', convert

Expand Down
9 changes: 8 additions & 1 deletion lib/calatrava/templates/kernel/plugins/alert.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
calatrava.alert = (message) ->
calatrava.bridge.plugins.call('alert', 'displayModal', {message: message})
calatrava.bridge.plugins.call 'alert', 'displayAlert',
message: message

calatrava.confirm = (message, onOkExecute) ->
okCallbackHandle = calatrava.bridge.plugins.rememberCallback(onOkExecute)
calatrava.bridge.plugins.call 'alert', 'displayConfirm',
message: message
okHandler: okCallbackHandle
10 changes: 7 additions & 3 deletions lib/calatrava/templates/web/app/source/alert.web.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
calatrava.web ?= {}

calatrava.web.alert = (method, {message}) ->
if method != 'runModal' || !message?
calatrava.web.alert = (method, {message, okHandler}) ->
if !message?
console.log("Unable to display alert.")

window.alert(message)
if method == 'displayAlert'
window.alert(message)
else if method == 'displayConfirm'
userPressedOk = window.confirm(message)
calatrava.bridge.runtime.invokePluginCallback(okHandler, userPressedOk)

calatrava.bridge.runtime.registerPlugin 'alert', calatrava.web.alert
3 changes: 3 additions & 0 deletions lib/calatrava/templates/web/app/source/bridge.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ calatrava.bridge.runtime = (() ->

callPlugin: (plugin, method, args) ->
plugins[plugin](method, args)

invokePluginCallback: (handle, data) ->
calatrava.inbound.invokePluginCallback(handle, data)
)()

0 comments on commit 731af09

Please sign in to comment.