Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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