1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- -module(testcities2).
- -include_lib("nitro/include/nitro.hrl").
- -include_lib("n4u/include/wf.hrl").
- %% example work with postgresql (with pool)
- -export([
- main/0,
- event/1
- ]).
- main() ->
- Tr = hm:get_tr_mod(), % get i18n module by lang
-
- Body = #dtl{file = "testcities", app = n2o_sample, bindings=[
- {cities, Tr:tr(testcities, cities) },
- {cities_show, Tr:tr(testcities, cities_show) },
- {cities_add, Tr:tr(testcities, cities_add) },
- {city_name, Tr:tr(testcities, city_name) },
- {city_pop, Tr:tr(testcities, city_pop) },
- {city_add, Tr:tr(testcities, city_add) }
- ]},
- #dtl{file = "layout", app = n2o_sample, bindings=[
- {page_title, Tr:tr(testcities, page_title) },
- {page_css, <<"<link rel=\"stylesheet\" href=\"/css/test.css\">">>},
- {page_body, wf:render(Body)},
- {page_js, <<"<script src=\"/js/testcities.js\" defer></script>">>}
- ]}.
- event(init) ->
- ?MODULE:event({client, {sitiesshow}});
- %[];
- event({client, {sitiesshow}}) ->
- Data = pq:get_all_cities(),
- InnerHtml = hg:generate_cities_list(Data),
- wf:wire(wf:f("var parent = qi('cities');"
- "parent.innerHTML='~s';"
- "window.getting_data=false;"
- "qi('citiesshow').disabled=false;", [unicode:characters_to_binary(InnerHtml, utf8)]));
- %% add one city and show all -- example with transaction
- event({client, {cityadd, Name, Pop}}) ->
- pgm:transaction(fun(Worker) ->
- %%epgsql:squery(Mpid, "BEGIN"), %% when transaction without pool
-
- {ok, R1} = pq:add_city_t(Worker, Name, Pop),
- hm:assert_int(R1),
-
- {ok, _Columns, Data} = pq:get_all_cities_t(Worker),
- %%hm:assert_int(R2)
-
- InnerHtml = hg:generate_cities_list(Data),
-
- wf:wire(wf:f("var parent = qi('cities');"
- "parent.innerHTML='~s';"
- "window.getting_data=false;"
- "qi('citiesshow').disabled=false;", [unicode:characters_to_binary(InnerHtml, utf8)])),
- ok
-
- %% commit pg-transaction - when transaction without pool
- %%epgsql:squery(Mpid, "COMMIT"),
- %% rollback pg-transaction - when transaction without pool
- %%epgsql:squery(Mpid, "ROLLBACK"),
- %%io:format("~p~n",[epgsql:get_cmd_status(Mpid)]),
- %%{ok,commit} or {ok, rollback}
- end);
- event(Event) ->
- wf:info(?MODULE, "Event: ~p", [Event]),
- ok.
|