Force page reload

31 views
Skip to first unread message

Anton Prokofiev

unread,
May 3, 2017, 7:36:35 AM5/3/17
to Nitrogen Project / The Nitrogen Web Framework for Erlang
Hello, All,

I use nitrogen as a front-end for the DB-driven application.
I have a list of items that are read from DB on page refresh.

User could edit/delete items.

My problem is following:

When user click DELETE button, my system delete record from DB.
Then page should be refreshed, to load latest status.

How could I trigger page reload from Nitrogen framework?

Thanks a lot in advance, Anton

Jesse Gumm

unread,
May 4, 2017, 8:37:14 AM5/4/17
to nitrogenweb
Hi Anton,

You have a few options here.


1) #sync_panel{}

The simplest would be to use a #sync_panel{}, which doesn't actually reload the page, but when it receives a message, refreshes the content of a panel as defined in a fun.

And a more detailed explanation are here: http://sigma-star.com/blog/post/sync_panel


2) Waiting for a comet message to force a literal page reload.  Something like this:

In your module:

refresh_dash() ->
    receive
        reload_dash ->
           wf:wire("location.reload()"),
           wf:flush();
        _ ->
           % just to purge accumulated messages that aren't our expected message
           ?MODULE:refresh_dash()  
    end.

main() ->
    wf:comet_global(my_dashboard, fun refresh_dash/0),
    #template{...}.


Then, in your code, whenever you save, or delete content related to that dash, you would issue:
wf:send_global(my_dashboard, reload_dash).


3) The lowest overall overhead on the server, but requires a fair bit more programming, and that's selectively deleting or adding items as they come in. For example.

body() ->
    Items = get_current_items_from_db(),
    wf:comet_global(my_dashboard, fun wait_for_updates/0),
    #panel{id=item_list, body=[
       [draw_item(Item) || Item <- Items]
    ]}.

draw_item(#{id:=ID, name:=Name}) ->
    ElementID = make_itemid(ID),
    #panel{id=ElementID, text=Name}.

make_itemid(ID) ->
    "item" ++ wf:to_list(ID).

wait_for_updates() ->
    receive
       {deleted, ID} ->
          wf:remove(make_itemid(ID));
       {new, Item} ->
          wf:insert_bottom(item_list, draw_item(Item));
       {updated, Item} ->
          #{id:=ID} = Item,
          wf:replace(make_itemid(ID), draw_item(Item));
       _ ->
         ok
    end,
    wf:flush(),
    ?MODULE:wait_for_updates().


Then when your item is deleted, added, or updated, you would send the appropriate comet_global, as follows:

Deleted:

wf:send_global(my_dashboard, {deleted, ID}),

Updated:

wf:send_global(my_dashboard, {updated, Item}),

Added:

wf:send_global(my_dashboard, {new, Item}).


That should do it.

When I'm doing something like this, I tend to use sync_panel first until I need a more precise solution, then I switch to something like (3).  It's pretty rare that I would actually reload the page, but for something like just a dashboard, it would be an okay solution.

Hope that helps,

-Jesse




--
You received this message because you are subscribed to the Google Groups "Nitrogen Project / The Nitrogen Web Framework for Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nitrogenweb+unsubscribe@googlegroups.com.
To post to this group, send email to nitro...@googlegroups.com.
Visit this group at https://groups.google.com/group/nitrogenweb.
For more options, visit https://groups.google.com/d/optout.



--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

anton prokofiev

unread,
May 4, 2017, 12:15:07 PM5/4/17
to nitro...@googlegroups.com
Thanks a lot.
Wf:wire("location:reload()")
Looks like exactly what I have been looking for.

Using sync panel is not my user case.
If I am not mistaken this will refresh panel for all users.
I am not shire that this is right
Solution, looks strange when your are working with data and they are refreshing continuously :)



Anton
You received this message because you are subscribed to a topic in the Google Groups "Nitrogen Project / The Nitrogen Web Framework for Erlang" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nitrogenweb/yPJ2SEZSJlM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nitrogenweb...@googlegroups.com.

Jesse Gumm

unread,
May 4, 2017, 2:47:43 PM5/4/17
to nitrogenweb
Hi Anton,

The #sync_panel{} relies on two levels of selectiveness when deciding when to trigger, so the content itself can be refreshed on a per-user, or per-role, or per-whatever basis.    The two levels are the global comet pool and a list of triggers (which are messages that when the process receives a message, will trigger a refresh).

For example, say you run a system with multiple subdomains (where each subdomain points to a different database), and within there, each subdomain's database has multiple users with different classes, we'll say a "securitylevel" (like admin or non-admin).

You can retrieve the subdomain by calling wf:header(host) (returns the full host-header, "ala mysite.mydomain.com"), and maybe you have a function usr:securitylevel() to retrieve the current user's securitylevel.

And let's say you only want a sync_panel to refresh for changes made within a subdomain and by the same securitylevel:

host_header() ->
    wf:header(host).

pool_name() ->
    {dashboard, host_header()}.

body() ->
   MySecuritylevel=usr:securitylevel(),
   #sync_panel{
      pool=pool_name(),
      triggers=[{update, MySecuritylevel}],
      body_fun=draw_dashboard/0
   }.

draw_dashboard() ->
   ...
      
      

Then when you want to refresh that list for those specific users, you would call:

element_sync_panel:refresh(pool_name(), {update, usr:securitylevel()}).

That would trigger the sync_panel to refresh for the users connected to only that one pool, and only the users with that securitylevel.

Make sense?

-Jesse


 

To unsubscribe from this group and all its topics, send an email to nitrogenweb+unsubscribe@googlegroups.com.

To post to this group, send email to nitro...@googlegroups.com.
Visit this group at https://groups.google.com/group/nitrogenweb.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Nitrogen Project / The Nitrogen Web Framework for Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nitrogenweb+unsubscribe@googlegroups.com.
To post to this group, send email to nitro...@googlegroups.com.
Visit this group at https://groups.google.com/group/nitrogenweb.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages