Skip to content

Instantly share code, notes, and snippets.

@zaphar
Created May 29, 2009 01:49
Show Gist options
  • Save zaphar/119723 to your computer and use it in GitHub Desktop.
Save zaphar/119723 to your computer and use it in GitHub Desktop.
-module (web_postback_test).
-include_lib ("nitrogen/include/wf.inc").
-compile(export_all).
main() ->
%TODO(jwall): force identity
#template { file="./wwwroot/api_test.html"}
.
test() ->
%% I need an ID for the hiden element I will use to communicate
%% values back to nitrogen with
DropBoxId = wf:temp_id()
%% Here is the event template we will use to post back the id we need to query
, Event = #event{type=click, postback={retrieve, DropBoxId}}
%% this javascript updates our hidden element with the value foo
, UpdateFooScript = wf:f("$('#~s')[0].value = 'foo';", [DropBoxId])
%% This javascript updates our hidden element with the value bar
, UpdateBarScript = wf:f("$('#~s')[0].value = 'bar';", [DropBoxId])
%% This javascript updates our hidden element with the value foobar
, UpdateFooBarScript = wf:f("$('#~s')[0].value = 'foobar';", [DropBoxId])
, #panel{ body=[
%% our hidden element to use to communicate back to nitrogen with
#hidden{id=DropBoxId, text="foobar"}
%% this button uses the base event to tell nitrogen
%% to query for the current value of the hidden element.
, #button{text="send back"
, actions=Event}
%% this button sets the value to foo and then tells nitrogen to query for it.
, #button{text="send foo back"
, actions=Event#event{actions=UpdateFooScript}}
%% this button sets the value to bar and then sends it back.
, #button{text="send bar back"
, actions=Event#event{actions=UpdateBarScript}}
%% this button sets the value to foobar and then sends it back
, #button{text="send foobar back"
, actions=Event#event{actions=UpdateFooBarScript}}
]}
.
event({retrieve, Id}) ->
%% this event tells nitrogen to query the given id for the current value.
io:format("querying: [~p]", [Id]),
Value = case wf:q(Id) of
[] -> "";
[V] -> V
end
, wf:update(log_spot, Value);
event(E) ->
io:format("recieved event: [~p]", [E])
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment