These are chat archives for synrc/n2o

16th
Jun 2016
Maruthavanan Subbaryan
@marutha
Jun 16 2016 04:42
wf:redirect("path") should redirect to path? Should we also do something else as well?
main() ->
    User = wf:user(),
    io:fwrite("~p~n", [User]),
    case User of
        <<>> -> wf:redirect("/login");
        undefined -> wf:redirect("/login");
        User ->
            [#dtl{file="app", ext="dtl", bindings = [{body, body()}]}]
    end.
This provides me the below result
maru@maru:~$ curl -I  http://localhost:8000/app
HTTP/1.1 200 OK
connection: keep-alive
server: Cowboy
date: Thu, 16 Jun 2016 04:43:46 GMT
content-length: 0
Maruthavanan Subbaryan
@marutha
Jun 16 2016 05:03
I think we need the n2o script to be run for wf:redirect/1 to work.
Maruthavanan Subbaryan
@marutha
Jun 16 2016 06:15
Can I do something like wf:update(element_id, #dtl{file = something, bindings = [{user, User}]}) ???
Maruthavanan Subbaryan
@marutha
Jun 16 2016 06:40
I have to do something like this for the above wf:jse(wf:render(DTL)) instead of passing #dtl{}
skatt
@skatt
Jun 16 2016 07:40

@marutha Yeah, there may be a better way, but I setup a simple function using a minimal DTL...

redirect(Route) -> wf:redirect(Route),[#dtl{file="redirect",app=web,bindings=[]}].

The DTL only has what is necessary for redirect to function.

Maruthavanan Subbaryan
@marutha
Jun 16 2016 07:46
Any guide how should one approach a big SPA with n2o?
skatt
@skatt
Jun 16 2016 07:55
Start simple. Setup the layout that will appear without any conditional requirements. Let it load no matter what. Then modify elements using update.
Still make different modules for each "work flow" that will be used on the SPA.
skatt
@skatt
Jun 16 2016 08:00
You can also load style elements in the body with new elements. This helps cut down on the initial stylesheet. I made an app specifically for managing styles and the dtl for it reads *.css templates where I can bind the generated IDs, my media query breakpoints, various colors and sizes, etc.
I also have a separate app for handling all of my forms and common element structures. This way my web app pretty much has the pages and router.
Also, make use of wf:cache for maintaining various data.
skatt
@skatt
Jun 16 2016 08:07
I use skel.js for my front-end. It lets me tell n2o what is going on with the client. Very helpful.
skatt
@skatt
Jun 16 2016 08:16

I also made an event router to direct events that are triggered from different modules to the same modules that trigger them. This way my code stays together and my page events don't become long and hard to read...
Page Module:

event(Event) -> wf:info(?MODULE, "Unknown event: Passing ~p to Nexus.", [Event]), event_router:event(?MODULE, Event).

event_router.erl

event(Module,{route,M,Event}) -> wf:info(Module, "Routed event: ~p", [{M,Event}]), M:event(Event);
This is okay since wf functions operate on the active page regardless of the module that calls them. If you want multi-user concurrency, then you need to use the process registry.
Maruthavanan Subbaryan
@marutha
Jun 16 2016 09:36
@skatt Thanks a lot the information, I am quite new to web development, but I kind of understood more, I might be asking more questions later, I am alway refering the samples directory of n2o, is there a better code sample somewhere in github?
@skatt I know it is too early and I am just curious to know, can I get the value of the URL that occurs after hash in n2o? I mean http://localhost/my_web_app/#this_value_after_hash
Ryuuji Kagami
@kagami-ryuuji
Jun 16 2016 10:04
@marutha Is hash value required? Maybe history api would be better?
skatt
@skatt
Jun 16 2016 10:04
That's called a fragment, fyi. I've never tried grabbing it. One of these probably will get it for you. I don't have anything open to check.
cowboy_req:binding
cowboy_req:header
cowboy_req:qs
https://github.com/ninenines/cowboy/blob/master/src/cowboy_req.erl
cowboy_req:qs_val* in proper version of cowboy...
https://github.com/ninenines/cowboy/blob/1.1.x/src/cowboy_req.erl
skatt
@skatt
Jun 16 2016 10:10
Best way to find out is to have page output cowboy_req:bindings/1
Req=Ctx#cx.req,

    {Bindings,_}=cowboy_req:bindings(Req),
    wf:info(?MODULE,"ROUTE Bindings: ~p",[Bindings])
Maruthavanan Subbaryan
@marutha
Jun 16 2016 10:55
@kagami-ryuuji I am not sure I would need or not at this point of time but still looking for some options on how we can do certain things with n2o
@skatt thanks..that was too early but still I will give a try
Matti Katila
@mudyc
Jun 16 2016 11:36
@skatt i don't think fragment is even part of the request that browser sends. i recall it is application specific fragment
skatt
@skatt
Jun 16 2016 11:37
I thought maybe that was the case, but I don't have anything open to check. qs is better choice imo, or just using cache with postback events.