Difference between revisions of "Namecoin SPA"
m |
m |
||
| Line 47: | Line 47: | ||
This level of functionality now gives us the ability to define an [[Ontology]], i.e. a triple-space of nodes with a reasonable number of relationships to other nodes (remember the JSON description must currently stay under 520 bytes). | This level of functionality now gives us the ability to define an [[Ontology]], i.e. a triple-space of nodes with a reasonable number of relationships to other nodes (remember the JSON description must currently stay under 520 bytes). | ||
| − | It then uses ''$d'' to declare a new function from the "od_node" domain which defines some fundamental functionality for a small group of domains that will be treated as | + | It then uses ''$d'' to declare a new function from the "od_node" domain which defines some fundamental functionality for a small group of domains that will be treated as nodes. |
{{code|<js>function $v(n,f){$.ajax({type:"GET",url:"/"+n,success:function(v){f(v);}});} | {{code|<js>function $v(n,f){$.ajax({type:"GET",url:"/"+n,success:function(v){f(v);}});} | ||
function $d(n,f){$v(n,function(v){eval("$d."+n+"=function(x){"+v+"};");});} | function $d(n,f){$v(n,function(v){eval("$d."+n+"=function(x){"+v+"};");});} | ||
| Line 54: | Line 54: | ||
| − | + | ;od_node | |
| − | + | After ''od_node'' is executed, all further program execution will be controlled by the relationships between nodes. | |
''od_spa'' (Single Page Application) domain, along with an anonymous function that well execute the new ''od_spa'' function after it has been received and declared. | ''od_spa'' (Single Page Application) domain, along with an anonymous function that well execute the new ''od_spa'' function after it has been received and declared. | ||
Revision as of 18:16, 26 August 2012
I'm making a Single Page Application which exists entirely inside the Namecoin network's name:value storage system. Values can only be a maximum size of one kilobyte, so the boot-strapping has to be very modular. In fact it turns out that the current version has a bug that prevents values from being updated if the current value is greater than 520 bytes! There's discussion about raising the limit at some point to around 9KB, but incurring steeper transaction costs for updates greater than 1KB.
od_init is the first node which declares two functions, the first called "v" takes a namecoin domain name and returns the content of its value field, or "404" if no such name was found. The second function called "d" extends the first function be taking one or more namecoin domain names and treating their values as the content of functions thats it declares. After the two functions are declared, it then calls "d" with "od_http" as the parameter to declare the contents of that domain as a function, and then calls that function.
od_http is a small server loop which listens for HTTP requests on port 2012, it declares functions from two other domains, "od_send" and "od_recv" which are called within the loop. The od_recv function is called when the server loop detects that input needs to be processed, the result will always be a domain name which is then passed through the "v" function to get its value, and this value is sent back to the client as an HTTP response message by od_send.
od_send composes the passed content into an HTTP message and sends it to the client.
od_recv reads in the GET request text and extracts just the valid domain part of it if any. If no domain is requested, then an HTML page is constructed from three other domains, "od_html" (an HTML DOCTYPE definition and opening element), "od_head" (meta tags, CSS and useful JS frameworks) abd "od_js" which is the next stage of the applications execution.
od_html is a standard DOCTYPE definition and opening HTML element for our applications page.
od_head is the HTML head element for our page including jQuery and jQueryUI which are practically prerequisites for any kind of web application these days.
od_js is the initial script that gives us the ability to declare new functionality from other namecoin domain values, it defines $v and $d functions which perform the same role as the initial Perl stage. In this case the $v function is passed a domain name and a function references, it retrieves the value of the name from the Perl server via Ajax, and then executes the passed function when the value is returned.
The $d is also passed a name and a function reference, the name is passed to $v along with a function that declares a new function of the same name as the domain with its content being the value of the name. The new function is declared inside the $d function-object so it can be accessed from any scope.
The $n (node) function is more high-level, like the previous two, its accepts a domain name and a callback function to deal with the resulting value (obtained via $v), but the result is treated as JSON data and stored as an object inside $n. The $n function looks in a property called "map" for the properties it will store for the node, the reason being that the JSON is then compatible with the namecoin network's method of encoding relationships between domains. If the JSON was not valid or there was no "map" property, an empty object will be stored. The resulting object is then passed to the processing function to handle.
This level of functionality now gives us the ability to define an Ontology, i.e. a triple-space of nodes with a reasonable number of relationships to other nodes (remember the JSON description must currently stay under 520 bytes).
It then uses $d to declare a new function from the "od_node" domain which defines some fundamental functionality for a small group of domains that will be treated as nodes.
);}
function $d(n,f){$v(n,function(v){eval("$d."+n+"=function(x){"+v+"};");});} function $n(n,f){v=$v(n,function(v){$n[n]={};try{j=$.parseJSON(v)}catch(exception){j={}}if('map'in j){for(i in j.map)$n[n][i]=j.map[i]};f($n[n]);});} $d('od_node',function(){$d.od_node();});</js>}}
- od_node
After od_node is executed, all further program execution will be controlled by the relationships between nodes.
od_spa (Single Page Application) domain, along with an anonymous function that well execute the new od_spa function after it has been received and declared.
od_spa is the initial structure of our Single Page Application, defining the event-handler for changes in the hash-fragment, and maintaining a local cache of domain content



