Wt examples
4.2.2
builddir
build
BUILD
wt-4.2.2
examples
javascript
JavascriptExample.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
#include <iostream>
7
8
#include <Wt/WApplication.h>
9
#include <Wt/WBreak.h>
10
#include <Wt/WContainerWidget.h>
11
#include <Wt/WText.h>
12
#include <Wt/WPushButton.h>
13
14
#include "
JavascriptExample.h
"
15
#include "
Popup.h
"
16
17
JavascriptExample::JavascriptExample
(
const
WEnvironment
& env)
18
:
WApplication
(env)
19
{
20
setTitle
(
"Javascript example"
);
21
22
// Create a popup for prompting the amount of money, and connect the
23
// okPressed button to the slot for setting the amount of money.
24
//
25
// Note that the input provided by the user in the prompt box is passed as
26
// an argument to the slot.
27
promptAmount_
=
Popup::createPrompt
(
"How much do you want to pay?"
,
""
);
28
promptAmount_
->okPressed().connect(std::bind(&
JavascriptExample::setAmount
,
29
this
, std::placeholders::_1));
30
31
// Create a popup for confirming the payment.
32
//
33
// Since a confirm popup does not allow input, we ignore the
34
// argument carrying the input (which will be empty anyway).
35
confirmPay_
=
Popup::createConfirm
(
""
);
36
confirmPay_
->okPressed().connect(
this
, &
JavascriptExample::confirmed
);
37
38
root
()->
addWidget
(cpp14::make_unique<WText>(
"<h2>Wt Javascript example</h2>"
39
"<p>Wt makes abstraction of Javascript, and therefore allows you"
40
" to develop web applications without any knowledge of Javascript,"
41
" and which are not dependent on Javascript."
42
" However, Wt does allow you to add custom Javascript code:</p>"
43
" <ul>"
44
" <li>To call custom JavaScript code from an event handler, "
45
"connect the Wt::EventSignal to a Wt::JSlot.</li>"
46
" <li>To call C++ code from custom JavaScript, use "
47
"Wt.emit() to emit a Wt::JSignal.</li>"
48
" <li>To call custom JavaScript code from C++, use "
49
"WApplication::doJavascript() or Wt::JSlot::exec().</li>"
50
" </ul>"
51
"<p>This simple application shows how to interact between C++ and"
52
" JavaScript using the JSlot and JSignal classes.</p>"
));
53
54
currentAmount_
55
=
root
()->
addWidget
(cpp14::make_unique<WText>(
"Current amount: $"
+
56
promptAmount_
->defaultValue()));
57
58
auto
amountButton =
59
root
()->
addWidget
(cpp14::make_unique<WPushButton>(
"Change ..."
));
60
amountButton->setMargin(10, Side::Left | Side::Right);
61
62
root
()->
addWidget
(cpp14::make_unique<WBreak>());
63
64
auto
confirmButton =
65
root
()->
addWidget
(cpp14::make_unique<WPushButton>(
"Pay now."
));
66
confirmButton->setMargin(10, Side::Top | Side::Bottom);
67
68
// Connect the event handlers to a JSlot: this will execute the JavaScript
69
// immediately, without a server round trip.
70
amountButton->clicked().connect(
promptAmount_
->show);
71
confirmButton->clicked().connect(
confirmPay_
->show);
72
73
// Set the initial amount
74
setAmount
(
"1000"
);
75
}
76
77
void
JavascriptExample::setAmount
(
const
std::string amount)
78
{
79
// Change the confirmation message to include the amount.
80
confirmPay_
->setMessage(
"Are you sure you want to pay $"
+ amount +
" ?"
);
81
82
// Change the default value for the prompt.
83
promptAmount_
->setDefaultValue(amount);
84
85
// Change the text that shows the current amount.
86
currentAmount_
->
setText
(
"Current amount: $"
+
promptAmount_
->defaultValue());
87
}
88
89
void
JavascriptExample::confirmed
()
90
{
91
root
()->
addWidget
(cpp14::make_unique<WText>(
"<br/>Just payed $"
+
92
promptAmount_
->defaultValue() +
"."
));
93
}
94
95
std::unique_ptr<WApplication>
createApplication
(
const
WEnvironment
& env)
96
{
97
return
cpp14::make_unique<JavascriptExample>(env);
98
}
99
100
int
main
(
int
argc,
char
**argv)
101
{
102
return
WRun(argc, argv, &
createApplication
);
103
}
104
Popup.h
Wt::WApplication::root
WContainerWidget * root() const
JavascriptExample::setAmount
void setAmount(std::string amount)
Set the amount to be payed.
Definition:
JavascriptExample.C:77
Wt::WApplication
JavascriptExample::confirmPay_
std::unique_ptr< Popup > confirmPay_
Popup for paying.
Definition:
JavascriptExample.h:46
main
int main(int argc, char **argv)
Definition:
JavascriptExample.C:100
Wt::WContainerWidget::addWidget
virtual void addWidget(std::unique_ptr< WWidget > widget)
JavascriptExample::promptAmount_
std::unique_ptr< Popup > promptAmount_
Popup for changing the amount.
Definition:
JavascriptExample.h:42
Wt::WApplication::setTitle
void setTitle(const WString &title)
JavascriptExample.h
Wt::WText::setText
bool setText(const WString &text)
Popup::createConfirm
static std::unique_ptr< Popup > createConfirm(const WString &message)
Create a confirm dialog.
Definition:
Popup.C:73
Wt::WEnvironment
JavascriptExample::JavascriptExample
JavascriptExample(const WEnvironment &env)
Create the example application.
Definition:
JavascriptExample.C:17
createApplication
std::unique_ptr< WApplication > createApplication(const WEnvironment &env)
Definition:
JavascriptExample.C:95
JavascriptExample::currentAmount_
WText * currentAmount_
WText for showing the current amount.
Definition:
JavascriptExample.h:50
JavascriptExample::confirmed
void confirmed()
The user has confirmed the payment.
Definition:
JavascriptExample.C:89
Popup::createPrompt
static std::unique_ptr< Popup > createPrompt(const WString &message, const std::string defaultValue)
Create a prompt dialog with the given default value.
Definition:
Popup.C:83
Generated on Fri Mar 20 2020 for
the C++ Web Toolkit (Wt)
by
1.8.17