-
-
Save trigeek38/fa8adcbaeb79e02db1a8 to your computer and use it in GitHub Desktop.
Some render update examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example #1: | |
event({submit, {new_message, Args}, _TriggerId, _TargetId}, Context) -> | |
UserId = proplists:get_value(userid, Args), | |
Message = z_context:get_q("chat_message", Context), | |
mod_signal:emit({insert_new_message, [{msg, Message}, {userid, UserId}]}, Context), | |
z_render:wire([ {reset, [{target, "global-chat-form"}]}, | |
{focus, [{target, "chat_message"}]} | |
], Context); | |
%% How it's called in the template | |
%% {% wire id="global-chat-form" type="submit" postback={new_message userid=m.acl.user} delegate="hotseat" %} | |
% Notice using proplists to get values from template variable vs. get_q for variable from query args | |
% z_render:wire normally takes the same variables as those in the template language as a list of proplists | |
Example #2: | |
event({postback, {mark_correct, Args}, _TriggerId, _TargetId}, Context) -> | |
QuestionId = proplists:get_value(id, Args), | |
ChoiceId = proplists:get_value(choice_id, Args), | |
Props = [{answer, ChoiceId}], | |
case m_rsc:update(QuestionId, Props, Context) of | |
{ok, QuestionId} -> | |
z_render:wire([ | |
{update,[ {target, "choice-sorter-" ++ z_convert:to_list(QuestionId)}, | |
{template, "_choice_list.tpl"}, | |
{id, QuestionId}]} | |
],Context); | |
_ -> z_render:growl("Error selecting answer", "error", true, Context) | |
end; | |
Example #3: | |
event({postback, {redirect, Args}, _TriggerId, _Target_id}, Context) -> | |
SessionPid = Context#context.session_pid, | |
Dispatch = {dispatch, z_convert:to_atom(proplists:get_value(dispatch, Args, "home"))}, | |
case proplists:get_value(id, Args) of | |
undefined -> Dispatch1 = [Dispatch]; | |
Id -> Dispatch1 = [Dispatch,{id, Id}] | |
end, | |
?DEBUG(Dispatch1), | |
Script = z_script:get_script(z_render:wire([ {redirect, Dispatch1}], Context)), | |
F3 = fun(X, Acc) -> case X == SessionPid of true -> Acc; _ -> [X|Acc] end end, | |
Pids = z_session_manager:fold(F3, [], Context), | |
[z_session:add_script(Script, Pid) || Pid <- Pids], | |
Context; | |
%% How it's call from a template | |
%% {% button text="Home" class="btn btn-success" action={postback | |
%% postback={redirect dispatch="home"} | |
%% delegate="hotseat"} | |
%% Notice how you can use the ?DEBUG macro | |
%% Notice how you can fold over all the session pids and send scripts to each one of them |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment