Common Pitfalls
The API aims to be easy to use and hard to misuse, however there are still some pitfalls saucer can't detect.
This page tries to document the most common mistakes and how to avoid them.
Execution Order Matters
The order in which you call some methods matters!
Common mistakes that fall under this category are those, in which you call set_url
(or serve
) before expose
.
Example: Ill formed program
#include <saucer/smartview.hpp>
int main()
{
saucer::smartview webview;
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.serve("index.html");
webview.expose(
"add_random",
[&](float number) {
auto random = webview.eval<float>("Math.random()").get();
return number + random;
},
true);
webview.show();
webview.run();
return 0;
}
In this case, it is not guaranteed that the function add_random
is available to the web-page immediately.
To fix this issue simply re-order the code.
Example: Well formed program
int main()
{
saucer::smartview webview;
webview.set_size(500, 600);
webview.set_title("Hello World!");
webview.serve("index.html");
webview.expose(
"add_random",
[&](float number) {
auto random = webview.eval<float>("Math.random()").get();
return number + random;
},
true);
webview.serve("index.html");
webview.show();
webview.run();
return 0;
}