mirror of
https://github.com/zaphar/jeremy.marzhillstudios.com.git
synced 2025-07-21 19:29:48 -04:00
Fix code formatting for a bunch of old articles.
This commit is contained in:
parent
4e6ca4fb57
commit
93e55a5478
@ -13,20 +13,25 @@ tags = [
|
||||
]
|
||||
+++
|
||||
In my <a href="http://jeremy.marzhillstudios.com/index.php/site-news/creating-custom-nitrogen-elements/">last post</a> I walked you through creating a basic nitrogen element. In this one I'll be covering some of the more advanced topics in nitrogen elements. <ul> <li>Event handlers and delegation</li> <li>scripts and dynamic javascript postbacks</li> </ul> <h3>Nitrogen Event handlers</h3> Nitrogen event handlers get called for any nitrogen event. A nitrogen event is specified by assigning #event to an actions attribute of a nitrogen element. The event handler in the pages module will get called with the postback of the event. Postbacks are an attribute of the event record and define the event that was fired. To handle the event you create an event function in the target module that matches your postback. For Example:
|
||||
<code syntax="erlang">
|
||||
|
||||
````erlang
|
||||
% given this event
|
||||
#event{ type=click, postback={click, Id} }
|
||||
% this event function would handle it
|
||||
event({click, ClickedId}) ->
|
||||
io:format("I [~p] was clicked", [ClickedId]) .
|
||||
</code>
|
||||
````
|
||||
|
||||
Erlangs pattern matching makes it especially well suited for this kind of event based programming. The one annoying limitation of this event though is that each page has to handle it individually. You could of course create a dispatching module that handled the event for you but why when nitrogen already did it for you. You can delegate an event to a specific module by setting the delegate attribute to the atom identifying that module.
|
||||
<code syntax="erlang">
|
||||
|
||||
```erlang
|
||||
% delgated event
|
||||
#event{ type=click, postback={click, Id}, delegate=my_module }
|
||||
</code>
|
||||
```
|
||||
|
||||
You can delgate to any module you want. I use the general rule of thumb that if the event affects other elements on the page then the page module should probably handle it. If, however, the event doesn't affect other elements on the page then the element's module can handle it. <h3>Scripts and Dynamic Postback</h3> Now lets get make it a little more interesting. Imagine a scenario where we want to interact with some javascript on a page and dynamically generate data to send back to nitrogen. As an example lets create a silly element that grabs the mouse coordinates of a click on the element and sends that back to nitrogen. A first attempt might look something like so:
|
||||
<code syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-record(silly, {?ELEMENT_BASE(element_silly)}). </code>
|
||||
And the module is likewise simple:
|
||||
<code syntax="erlang">
|
||||
@ -46,13 +51,18 @@ event({click, Loc}) ->
|
||||
</code>
|
||||
Well of course you spot the problem here. Since the click happens client side we don't know what to put in the Loc variable for the postback. A typical postback won't work because the data will be generated in the client and not the Nitrogen server. So how could we get the value of the coordinates sent back? The javascript to grab the coordinates with jquery looks like this = "<code syntax="javascript">"
|
||||
var coord = obj('me').pageX + obj('me').pageY;
|
||||
</code> To plug that in to the click event is pretty easy since action fields in an event can hold other events or javascript or a list combining both:
|
||||
<code syntax="erlang">
|
||||
```
|
||||
|
||||
To plug that in to the click event is pretty easy since action fields in an event can hold other events or javascript or a list combining both:
|
||||
|
||||
```erlang
|
||||
Script = "var coord = obj('me').pageX + obj('me').pageY;",
|
||||
ClickEvent = #event{type=click, postback={click, Loc}, actions=Script}
|
||||
</code>
|
||||
```
|
||||
|
||||
Now we've managed to capture the coordinates of the mouse click, but we still haven't sent it back to the server. This javascript needs a little help. What we need is a drop box. Lets enhance our element with a few helpers:
|
||||
<code syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-module(element_silly).
|
||||
-compile(export_all).
|
||||
-include("elements.hrl").
|
||||
@ -73,5 +83,8 @@ render(ControlId, R) ->
|
||||
event({click, Id, Msg}) ->
|
||||
Loc = hd(wf:q(Id)),
|
||||
wf:update(Msg, wf:f("you clicked at point = "~s", Loc))."
|
||||
</code>
|
||||
Ahhh there we go. Now our element when clicked will: <ol> <li>use javascript to grab the coordinates of the mouse click</li> <li>use javascript to store those coordinates in the hidden element</li> <li>use a postback to send the click event back to a nitrogen event handler with the id of the hidden element where it stored the coordinates.</li> </ol> We have managed to grab dynamically generated data from the client side and drop it somehwere that nitrogen can retrieve it. In the process we have used an event handler, custom javascript, and dynamic javascript postbacks. <strong>Edit</strong> = "Corrected typo - June 16, 2009 at 11:40 pm "
|
||||
```
|
||||
|
||||
Ahhh there we go. Now our element when clicked will: <ol> <li>use javascript to grab the coordinates of the mouse click</li> <li>use javascript to store those coordinates in the hidden element</li> <li>use a postback to send the click event back to a nitrogen event handler with the id of the hidden element where it stored the coordinates.</li> </ol> We have managed to grab dynamically generated data from the client side and drop it somehwere that nitrogen can retrieve it. In the process we have used an event handler, custom javascript, and dynamic javascript postbacks.
|
||||
|
||||
<strong>Edit</strong> = "Corrected typo - June 16, 2009 at 11:40 pm "
|
||||
|
@ -16,18 +16,22 @@ tags = [
|
||||
]
|
||||
+++
|
||||
<a href="http://github.com/rklophaus/nitrogen/tree/master">Nitrogen</a> is a web framework written in erlang for Fast AJAX Web applications. You can get <a href="http://github.com/rklophaus/nitrogen/tree/master">Nitrogen on github</a> Nitrogen comes with a set of useful controls, or elements in nitrogen parlance, but if you are going to do anything more fancy than a basic hello world you probably want to create some custom controls. This tutorial will walk you through the ins and outs of writing a custom element for Nitrogen. We will be creating a simple notification element similar to one I use in the Iterate! project. It will need to be able to: <ul> <li> show a message</li> <li>have a way to dismiss it</li> <li> and optionally expire and disappear after a configurable period of time</li> </ul> Every Nitrogen element has two main pieces = "the Record and the Module. I'll go through each in order and walk you through creating our notification element. <h2>The Record</h2> The record defines all the state required to create a nitrogen element. Every record needs a certain base set of fields. These fields can be added to your record with the ?ELEMENT_BASE macro. The macro is available in the nitrogen include file wf.inc. That include file also gives you access to all the included nitrogen element records. Below you can see the record definition for our notify element. Since it is very simple in it's design it only needs the base elements and two additional fields. expire to handle our optional expiration time and default to false to indicate no expiration. msg to hold the contents of our notification."
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
%Put this line in an include file for your elements
|
||||
-record(notify, {?ELEMENT_BASE(element_notify), expire=false, msg}).
|
||||
</pre>
|
||||
<pre syntax="erlang">
|
||||
```
|
||||
|
||||
```erlang
|
||||
% put these at the top of your elements module
|
||||
-include_lib("nitrogen/include/wf.inc").
|
||||
% the above mentioned include file you may call it whatever you want
|
||||
-include("elements.inc").
|
||||
</pre>
|
||||
```
|
||||
|
||||
The ELEMENT_BASE macro gives your element several fields and identifies for the element which module handles the rendering of your nitrogen element. You can specify any module you want but the convention is to name the module with element_element_name. The fields provided are: id, class, style, actions, and show_if. You can use them as you wish when it comes time to render your element. Which brings us to the module. <h2>The Module</h2> Of the two pieces of a nitrogen element the module does the manual labor. It renders and in some cases defines the handlers for events fired by the element. The module must export a render/2 function. This function will be called whenever nitrogen needs to render a particular instance of your element. It's two arguments are = "The ControlId, and the Record defining this element instance. Of these the ControlID is probably the least understood. It is passed into your render method by nitrogen and is the assigned HTML Id for your particular element. This is important to understand because, when you call the next render method in your elements tree, you will have to pass an ID on. The rule of thumb I use is that if you want to use a different Id for your toplevel element then you can ignore the ControlId. Otherwise you should use it as the id for your toplevel element in the control. So your element's module should start out with something like this:"
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-module(element_notify).
|
||||
-compile(export_all).
|
||||
-include_lib("nitrogen/include/wf.inc").
|
||||
@ -46,27 +50,35 @@ render(ControlId, R) ->
|
||||
% Or use the alternative method:
|
||||
Module = Panel#panel.module,
|
||||
Module:render(Id, Panel).
|
||||
</pre>
|
||||
```
|
||||
|
||||
Notice that the records module attribute tells us what module we should call to render the element in the alternative method. In our case we will just hardcode the module since it's known to us. So now we have a basic element that renders a div with a temp id to our page. That's not terribly useful though. We actually need this element to render our msg, and with some events attached. Lets add the code to add our message to the panels contents.
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
Panel = #panel{id=Id, body=R#notify.msg},
|
||||
element_panel:render(ControlId, Panel)
|
||||
</pre>
|
||||
```
|
||||
|
||||
Now whatever is in the msg attribute of our notify record will be in the body of the panel when it gets rendered. All we need is a way to dismiss it. A link should do the trick. But now we have a slight problem. In order to add our dismiss link we need to add it to the body of the Panel. but the msg is already occupying that space. We could use a list and prepend the link to the end of the list for the body but that doesn't really give us a lot of control over styling the element. what we really need is for the msg to be in an inner panel and the outer panel will hold any controls the element needs.
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
Link = #link{text="dismiss"},
|
||||
InnerPanel = #panel{body=R#notify.msg},
|
||||
Panel = #panel{id=Id, body=[InnerPanel,Link]},
|
||||
element_panel:render(ControlId, Panel)
|
||||
</pre>
|
||||
```
|
||||
|
||||
Our link doesn't actually dismiss the notification yet though. To add that we need to add a click event to the link. Nitrogen has a large set of events and effects available. You can find them . We will be using the click event and the hide effect.
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
Event = #event{type=click,
|
||||
actions=#hide{effect=blind, target=Id}},
|
||||
Link = #link{text="dismiss", actions=Event},
|
||||
</pre>
|
||||
```
|
||||
|
||||
Now our module should look something like this:
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-module(element_notify).
|
||||
-compile(export_all).
|
||||
-include_lib("nitrogen/include/wf.inc").
|
||||
@ -85,9 +97,12 @@ render(ControlId, R) ->
|
||||
Panel = #panel{id=Id, body=[InnerPanel,Link]},
|
||||
% the element_panel module is used to render the panel element
|
||||
element_panel:render(Id, Panel).
|
||||
</pre>
|
||||
```
|
||||
|
||||
This is a fully functional nitrogen element. But it's missing a crucial feature to really shine. Our third feature for this element was an optional expiration for the notification. Right now you have to click dismiss to get rid of the element on the page. But sometimes we might want the element to go away after a predetermined time. This is what our expire record field is meant to determine for us. There are three possible cases for this field. <ul> <li>set to false (the default)</li> <li>set to some integer (the number of seconds after which we want to go away)</li> <li>set to anything else (the error condition)</li> </ul> This is the kind of thing erlang's case statement was made for:
|
||||
<pre syntax="erlang">
|
||||
|
||||
|
||||
```erlang
|
||||
case R#notify.expire of
|
||||
false ->
|
||||
undefined;
|
||||
@ -98,9 +113,11 @@ case R#notify.expire of
|
||||
% log error and don't expire
|
||||
undefined
|
||||
end
|
||||
</pre>
|
||||
```
|
||||
|
||||
Notice the wf:wire statement. wf:wire is an alternate way to add events to a nitrogen element. Just specify the id and then the event record/javascript string you want to use. I've noticed that for events of type timer wf:wire works better than assigning them to the actions field of the event record. No idea why because I have not looked into it real closely yet. Now our module looks like this:
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-module(element_notify).
|
||||
-compile(export_all).
|
||||
-include_lib("nitrogen/include/wf.inc").
|
||||
@ -129,14 +146,18 @@ render(_, R) ->
|
||||
Panel = #panel{id=Id, body=[InnerPanel,Link]},
|
||||
% the element_panel module is used to render the panel element
|
||||
element_panel:render(ControlId, Panel).
|
||||
</pre>
|
||||
```
|
||||
|
||||
We have now fulfilled all of our criteria for the element. It shows a message of our choosing. It can be dismissed with a click. And it has an optional expiration. One last thing to really polish it off though would to allow styling through the use of css classes. The ELEMENT_BASE macro we used in our record definition gives our element a class field. We can use that to set our Panel's class, allowing any user of the element to set the class as they wish like so:
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
Panel = #panel{id=Id, class=["notify ", R#notify.class],
|
||||
body=[InnerPanel,Link]},
|
||||
</pre>
|
||||
```
|
||||
|
||||
This gives us the final module for our custom element:
|
||||
<pre syntax="erlang">
|
||||
|
||||
```erlang
|
||||
-module(element_notify).
|
||||
-compile(export_all).
|
||||
-include_lib("nitrogen/include/wf.inc").
|
||||
@ -166,5 +187,6 @@ reflect() -> record_info(fields, notify).
|
||||
body=[InnerPanel,Link]},
|
||||
% the element_panel module is used to render the panel element
|
||||
element_panel:render(ControlId, Panel).
|
||||
</pre>
|
||||
```
|
||||
|
||||
I will cover delegated events and more advanced topics in a later tutorial.
|
||||
|
@ -27,7 +27,7 @@ For my purposes I ported the DynamicBible search box and a requirejs importer el
|
||||
Creating your own Polymer Element
|
||||
=================================
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="my-element">
|
||||
<template>
|
||||
...
|
||||
@ -40,7 +40,7 @@ Creating your own Polymer Element
|
||||
|
||||
The `<polymer-element>` is the declarative way to define your polymer element in html itself. The bare minimum to define a polymer element would be
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="search-box">
|
||||
<script>
|
||||
Polymer('search-box');
|
||||
@ -50,13 +50,13 @@ Polymer('search-box');
|
||||
|
||||
This is as about as useful as the span element though and html already has a span element. We need a little more than this to be worth it. Our element needs some attributes and behavior. Polymer lets us describe the expected attributes using an attribute called, what else, `attributes`.
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="search-box" attributes="id query">
|
||||
```
|
||||
|
||||
As for the behavior to attach to this element, that brings us the the Polymer construction function.
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="search-box" attributes="id query">
|
||||
<script>
|
||||
// Polymers construction function.
|
||||
@ -75,20 +75,20 @@ Polymer('search-box', {
|
||||
|
||||
You can use the element the same way you would any other html element.
|
||||
|
||||
``` html
|
||||
```html
|
||||
<search-box id="searchBox"></search-box>
|
||||
```
|
||||
|
||||
Now our element has a method on it that will submit a search using the value of our search-box query attribute. We could trigger this behavior right now with javascript.
|
||||
|
||||
``` js
|
||||
```js
|
||||
document.querySelector('#searchBox').query = "what is a polymer element?";
|
||||
document.querySelector('#searchBox').search();
|
||||
```
|
||||
|
||||
It's kind of silly that we have to do that manually with javascript though. What we really want is for this element to detect changes in our query and perform the search for us.
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="search-box" attributes="id query">
|
||||
<script>
|
||||
Polymer('search-box', {
|
||||
@ -117,7 +117,7 @@ Up to now our element hasn't been very visible. We need to give it an image boos
|
||||
|
||||
We'll go with the template element for now. The element inheritance will come in handy later.
|
||||
|
||||
``` html
|
||||
```html
|
||||
<polymer-element name="search-box" attributes="id query">
|
||||
<template>
|
||||
<input type="text" value="{{ query }}">
|
||||
@ -148,7 +148,7 @@ Out elements template element isn't terribly complicated and it turns out in our
|
||||
|
||||
Our last tweak to the search-box element looks like this.
|
||||
|
||||
``` html
|
||||
```html
|
||||
// input already defines the attributes we need
|
||||
<polymer-element name="search-box" extends="input">
|
||||
<script>
|
||||
|
@ -29,7 +29,7 @@ go-html-transform is an example of that last one. The basic theory is that an ht
|
||||
Example usage.
|
||||
=======
|
||||
|
||||
``` go
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
|
Loading…
x
Reference in New Issue
Block a user