Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 281 lines (230 sloc) 9.513 kb
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
1
2 <erl>
3
4
5 box(Str) ->
6 {'div',[{class,"box"}],
7 {pre,[], yaws_api:htmlize(Str)}}.
8
9 tbox(T) ->
10 box(lists:flatten(io_lib:format("~p",[T]))).
11
12
13 ssi(File) ->
14 {'div',[{class,"box"}],
15 {pre,[],
16 {ssi, File,[],[]}}}.
17
18 ss(A, File) ->
19 {ok, B} = file:read_file(
0be3c7e @klacke untabified all of yaws
authored
20 filename:join([A#arg.docroot, File])),
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
21 box(binary_to_list(B)).
22
23
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
24
25 out(A) ->
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
26 [{ssi, "TAB.inc", "%%",[{"json_intro", "choosen"}]},
27 {ehtml,
28 {'div', [{id, "entry"}],
29
30 [{h1, [], "AJAX through JSON RPC"},
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
31
0be3c7e @klacke untabified all of yaws
authored
32 {p, [],
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
33 {i, [],
0be3c7e @klacke untabified all of yaws
authored
34 ["Note: this documentation used to refer to the module "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
35 "'yaws_jsonrpc', but that module was deprecated in favor of "
36 "'yaws_rpc', which handles JSON RPC, haXe and SOAP remoting. "
37 "For more specific information about SOAP, refer to ",
0be3c7e @klacke untabified all of yaws
authored
38 {a, [{href, "/soap_intro.yaws"}], "the SOAP page."}]}},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
39 {p, [],
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
40 ["The Yaws JSON-RPC binding is a way to have JavaScript code in the "
41 "browser evaluate a remote procedure call (RPC) in the Yaws server. "
0be3c7e @klacke untabified all of yaws
authored
42 "JSON itself as described at ",
43 {a, [{href, "http://www.json.org/"}], "http://www.json.org/ "},
44 "is basically a simple marshaling format which can be used "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
45 "from a variety of different programming languages, and "
46 "naturally it's completely straightforward to implement "
47 "in JavaScript itself. JSON-RPC version 2.0, the version Yaws "
48 "supports, is described here:"]},
49 {p, [],
50 [{a, [{href,
51 "http://groups.google.com/group/json-rpc/web/json-rpc-2-0"}],
52 "http://groups.google.com/group/json-rpc/web/json-rpc-2-0"}]},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
53 {p, [],
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
54 "The Yaws JSON-RPC implementation consist of JavaScript clients and a "
55 "server side library that must be explicitly invoked by Erlang "
56 "code in a .yaws page, appmod, etc."},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
57
58 {p,[],
0be3c7e @klacke untabified all of yaws
authored
59 "It is not particularly easy to show and explain an AJAX setup "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
60 "through JSON-RPC, but here is an attempt:"
0be3c7e @klacke untabified all of yaws
authored
61 },
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
62 {p,[],
0be3c7e @klacke untabified all of yaws
authored
63 "First we have an HTML page which:"},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
64 {ol, [],
0be3c7e @klacke untabified all of yaws
authored
65 [
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
66 {li,[],{p,[],
67 ["Includes the client side of the JSON library. "
68 "The library is included in the Yaws distribution "
69 "and it is found under ",
70 {a,
71 [{href,
72 "https://github.com/klacke/yaws/blob/master/www/jsolait/jsolait.js"}],
73 "\"www/jsolait/jsolait.js\""}, "."]}},
74 {li,[],{p,[],"Second, the HTML code defines the name of a method, "
75 "i.e. the name of a server-side function that shall be "
76 "called by the client side JavaScript code."}},
77 {li,[],{p,[],"Finally the HTML code defines a FORM that's "
0be3c7e @klacke untabified all of yaws
authored
78 "used to invoke the RPC. This is just a really simple "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
79 "example, really any JavaScript code can invoke any RPC in "
80 "more interesting scenarios than submitting a form."}}]},
81
82 {p, [], "The HTML code appears as shown below:"},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
83 ss(A,"json_sample.html"),
84 {p, [], ["This HTML code resides in file ",
0be3c7e @klacke untabified all of yaws
authored
85 {a,[{href, "json_sample.html"}], "json_sample.html"},
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
86 " and it is the HTML code that is the AJAX GUI."]},
87 {p, [], "Following that we need to take a look at json_sample.yaws "
88 " (shown below), which is the \"serviceURL\" according to "
89 "the JavaScript code. This code defines the function to be "
90 "called. Remember that the JavaScript code defined one method, "
91 "called \"test1\"; this information will be passed to the "
0be3c7e @klacke untabified all of yaws
authored
92 "serviceURL. The code looks like:"},
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
93 ss(A, "json_sample.yaws"),
80f6d9f @klacke *** empty log message ***
authored
94
95 {p,[], "The two important lines on the server side are"},
96 {ol,[],
0be3c7e @klacke untabified all of yaws
authored
97 [
98 {li,[],
99 {pre,[],"yaws_rpc:handler_session(A2, {sample_mod, counter})."}},
100 {li,[],
101 {pre,[],"counter([{ip, IP}] = _State, {call, test1, Value} = _Request, Session)"}}]},
80f6d9f @klacke *** empty log message ***
authored
102
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
103 {p,[],
104 ["The first line tells Yaws to forward all JSON-RPC methods to the "
105 " \"counter\" function in the \"sample_mod\" module. "
106 "The second line is the head of the counter function that will be "
107 "called when the client invokes a method called 'test1'. We would "
108 "duplicate this line with a different name than 'test1' for each RPC "
109 "function we wish to implement. Note that the first atom in the "
110 "request tuple will either be 'call' or 'notification' to indicate "
111 "the type of request. As per the ",
112 {a,[{href,"http://groups.google.com/group/json-rpc/web/json-rpc-2-0"}],
113 "JSON-RPC 2.0 specification"},
114 ", a 'call' is a regular request-reply while a 'notification' is a "
115 "one-way message that does not have a corresponding reply."]},
116
80f6d9f @klacke *** empty log message ***
authored
117 {p,[],"On the client side we have"},
118
119 box("
120 var methods = [ \"test1\" ];
121 var jsonrpc = imprt(\"jsonrpc\");
122 var service = new jsonrpc.ServiceProxy(serviceURL, methods);
123 "),
124
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
125 {p,[],"This registers the Yaws page with the JSON-RPC handler and "
80f6d9f @klacke *** empty log message ***
authored
126 "gives it a list of methods that the Yaws page can satisfy. "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
127 "In this case, the only method called 'test1'."},
128
129 {p, [],
130 "When we wish to return structured data, we simply let "
131 "the user-defined RPC function return JSON structures such as "},
80f6d9f @klacke *** empty log message ***
authored
132
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
133 box("{struct, [{field1, \"foo\"}, {field2, \"bar\"}]} "),
80f6d9f @klacke *** empty log message ***
authored
134 {p, [], " for a structure and "},
135 box("{array, [\"foo\", \"bar\"]}"),
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
136 {p, [],"for an array. We can nest arrays and structs in each other."},
80f6d9f @klacke *** empty log message ***
authored
137
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
138 {p, [],
0be3c7e @klacke untabified all of yaws
authored
139 "Finally, we must stress that this example is extremely simple. "
140 "In order to build a proper AJAX application in Yaws, a lot of "
141 "client side work is required, all Yaws provides is the basic "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
142 "mechanism whereby the client side JavaScript code can RPC the "
0be3c7e @klacke untabified all of yaws
authored
143 "web server for data which can be subsequently used to populate "
144 "the DOM. Also required to build a good AJAX application is "
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
145 "good knowledge of how the DOM in the browser works"},
f7f7440 Added call to callback function: M:F(cookie_expire) which is expected…
Tobbe Tornquist authored
146
147 {p, [],
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
148 ["The yaws_rpc:handler will also call: M:F(cookie_expire) "
149 "which is expected to return a proper Cookie expire string. This "
150 "makes it possible to setup the Cookie lifetime. If this callback "
151 "function is non-existent, the default behaviour is to not set a "
152 "cookie expiration time, i.e., it will live for this session only."]},
d422225 @klacke some more json docs
authored
153
154 {h3, [], "One more example "},
155
156 {p, [],
157 ["Here is yet another example, stolen from ",
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
158 {a,
159 [{href,"http://www.redhoterlang.com/entry/ac061493b201e3d1b4490cdc3f911068"}],
160 "Tobbe's blog."}
d422225 @klacke some more json docs
authored
161 ]},
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
162 {h4, [], "Setup the DOM"},
163 {p, [], "In the file ''ex1.html'' we create the DOM with a little HTML and add some JavaScript that will talk with the Erlang server side."},
d422225 @klacke some more json docs
authored
164 box("
165
166 <html>
167 <head>
168 <script type=\"text/javascript\"
169 src=\"/jquery-1.2.3.js\"></script>
170 </head>
171 <body>
172
173 <script language=\"javascript\" type=\"text/javascript\">
174
175 function ex1(what) {
176 $.getJSON(\"/ex1.yaws\",
177 {'op': \"ex1\", 'what': what},
178 function(x) {
179 do_ex1(what, x)
180 });
181 }
182
183 function do_ex1(what, x) {
184 jQuery.each(x, doit);
185 }
186
187 function doit() {
188 $('#'+this.who).html(this.what);
189 }
190
191 </script>
192
193 <button onclick=\"ex1('one')\">Update one!</button>
194 <button onclick=\"ex1('two')\">Update two!</button>
195 <button onclick=\"ex1('three')\">Update three!</button>
196
197 <div id=\"one\">This is one</div>
198 <div id=\"two\">This is two</div>
199 <div id=\"three\">This is three</div>
200
201 </body>
202 </html>
203 "),
204 {h4, [], "The erlang server side"},
205 {p, [], "This is the code that needs to be installed and "
206 "execute on the server side. It nicely illustrates how "
207 "to return JSON structs to the client. "},
208 box("
209 -module(ex1).
210 -export([out/1]).
211
212 out(A) ->
213 L = yaws_api:parse_query(A),
214 dispatch(lkup(\"op\", L, false), A, L).
215
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
216 dispatch(\"ex1\", A, L) ->
d422225 @klacke some more json docs
authored
217 ex1(A, L).
218
219 ex1(_A, L) ->
96534b2 @vinoski replace json.erl with json2.erl to fix issue 50
vinoski authored
220 J = json2:encode(array(what(lkup(\"what\", L, false)))),
d422225 @klacke some more json docs
authored
221 return_json(J).
222
223 what(\"one\") -> one();
224 what(\"two\") -> one() ++ two();
225 what(\"three\") -> one() ++ two() ++ three().
226
227 array(L) -> {array, L}.
228
229 one() -> obj(\"one\").
230 two() -> obj(\"two\").
231 three() -> obj(\"three\").
232
233 obj(M) ->
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
234 obj(M, \"r\").
d422225 @klacke some more json docs
authored
235
236 %%%
237 %%% How ::= \"r\" | \"a\" , r=replace, a=append
238 %%%
239 obj(M, How) ->
240 C = now2str(),
241 [{struct,
242 [{\"who\", M},
243 {\"how\", How},
244 {\"what\", C ++\" \"++M++\" content\"}]}].
245
246 return_json(Json) ->
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
247 {content,
248 \"application/json; charset=iso-8859-1\",
d422225 @klacke some more json docs
authored
249 Json}.
250
251 now2str() ->
252 {A,B,C} = erlang:now(),
253 i2l(A)++\"-\"++i2l(B)++\"-\"++i2l(C).
254
255 i2l(I) when is_integer(I) -> integer_to_list(I);
256 i2l(L) when is_list(L) -> L.
257
258 lkup(Key, List, Def) ->
259 case lists:keysearch(Key, 1, List) of
260 {value,{_,Value}} -> Value;
261 _ -> Def
262 end.
263
264 ") ,
265 {h2, [], "The json library"},
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
266 {p, [],
267 ["The Yaws JSON library contains 3 simple functions, "
268 " one for encoding and two for decoding. See source code ",
269 {a,
270 [{href,
271 "https://github.com/klacke/yaws/blob/master/src/json2.erl"}],
272 "json2.erl"},
273 " for detailed instructions on usage."]}
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
274
275 ]}},
276
277 {ssi, "END2",[],[]}
278 ].
279
280 </erl>
Something went wrong with that request. Please try again.