Wt
4.11.0
|
A signal to relay JavaScript to C++ calls. More...
#include <Wt/WJavaScript.h>
Public Member Functions | |
JSignal (WObject *object, const std::string &name, bool collectSlotJavaScript=false) | |
Construct a signal for the given object, and name. | |
~JSignal () | |
Destructor. | |
const std::string & | name () const |
Returns the signal name. | |
const std::string | createCall (std::initializer_list< std::string > args) const |
Returns a JavaScript call that triggers the signal. | |
const std::string | createEventCall (const std::string &jsObject, const std::string &jsEvent, std::initializer_list< std::string > args) const |
Returns a JavaScript call that triggers the signal, passing the original event too. | |
virtual bool | isConnected () const override |
Returns whether the signal is connected to at least one slot. | |
template<class F > | |
Wt::Signals::connection | connect (F function) |
Connect to a function. | |
template<class T , class V , class... B> | |
Wt::Signals::connection | connect (T *target, void(V::*method)(B...)) |
Connect a slot that takes no arguments. | |
void | connect (const std::string &function) |
Connects a JavaScript function. | |
void | connect (JSlot &slot) |
Connect a slot that is specified as JavaScript only. | |
void | emit (A... args) const |
Emit the signal. | |
void | operator() (A... args) const |
Emit the signal. | |
virtual Wt::Signals::connection | connect (WObject *target, void(WObject::*method)()) override |
Connects to a slot. | |
Public Member Functions inherited from Wt::EventSignalBase | |
const char * | name () const |
Returns the event name. | |
void | disconnect (JSlot &slot) |
Disconnects a JSlot. | |
void | preventDefaultAction (bool prevent=true) |
Prevents the default browser action. | |
bool | defaultActionPrevented () const |
Returns whether the default browser action is prevented. | |
void | preventPropagation (bool prevent=true) |
Prevents event propagation. | |
bool | propagationPrevented () const |
Returns whether event propagation is prevented. | |
template<class T , class V > | |
Wt::Signals::connection | connect (T *target, void(V::*method)()) |
Connects to a slot. | |
Public Member Functions inherited from Wt::SignalBase | |
template<class T , class V > | |
Wt::Signals::connection | connect (T *target, void(V::*method)()) |
Connects to a slot. | |
A signal to relay JavaScript to C++ calls.
A JSignal, like an EventSignal, provides communicates events from JavaScript to C++ code. However, it not tied to a built-in event. Instead, it can be emitted from within custom JavaScript code using the JavaScript Wt.emit() function.
The signal is identified by a unique name within the scope of a WObject, or a unique global name (when declaring the signal in your WApplication).
The signal supports up to 6 arguments. Values for these arguments may be specified in the JavaScript Wt.emit().
Example code:
The following JavaScript statement will emit the signal for a DOM element element that corresponds to a widget of class MyWidget
:
The element can be a DOM element, or the object ID of a WObject, or the constant Wt
which refers to the Wt::WApplication instance. The conversion between the JavaScript arguments (ax) and the C++ type Ax uses stream operators.
You can use the methods createCall() to let the signal itself generate this JavaScript call for you:
The JavaScript generated by createCall() is possibly affected by every connect or disconnect to the signal. In practice, you will use JSignal internally within a widget and call createCall() only after you connected internal slots to signal.
It is also possible to propagate an original JavaScript event as a last argument, of type WMouseEvent, WKeyEvent or WTouchEvent. In that case, the second argument in Wt.emit()
must be an object which indicates also the JavaScript event and event target.
Consider a signal declaration:
Then, the following would be a suitable JavaScript call:
The method createEventCall() may be used this variation for the JavaScript method call.
Since the conversion from JavaScript to C++ uses stream operators, you may provide support for custom types by implementing the c++ input stream operator std::istream& operator>> (std::istream&, T& t)
for your type.
Wt::JSignal< A >::JSignal | ( | WObject * | object, |
const std::string & | name, | ||
bool | collectSlotJavaScript = false |
||
) |
Construct a signal for the given object, and name.
The given name
must be unique for all user signals specified for the object object
. Ownership of the signal is not transferred to the object.
If collectSlotJavaScript
is true
, then javascript specified for connected slots (using JSlot) or learned by stateless slot learning, is collected to client-side JavaScript.
Use the utility methods createCall() or createEventCall() to create the appropriate JavaScript statements that invoke the signal, which take into account possible other client-side JavaScript handling associated with the signal.
void Wt::JSignal< A >::connect | ( | const std::string & | function | ) |
Connects a JavaScript function.
This will provide a client-side connection between the event and a JavaScript function. The argument must be a JavaScript function which optionally accepts up to two arguments (object and event):
Unlike a JSlot, there is no automatic connection management: the connection cannot be removed. If you need automatic connection management, you should use connect(JSlot&) instead.
Wt::Signals::connection Wt::JSignal< A >::connect | ( | F | function | ) |
Connect to a function.
This variant of the overloaded connect() method supports a template function object (which supports operator ()).
When the receiver function is an object method, the signal will automatically be disconnected when the object is deleted, as long as the object inherits from WObject (or Wt::Signals::trackable).
void Wt::JSignal< A >::connect | ( | JSlot & | slot | ) |
Connect a slot that is specified as JavaScript only.
This will provide a client-side connection between the event and some JavaScript code as implemented by the slot. Unlike other connects, this does not cause the event to propagated to the application, and thus the state changes induced by the slot
are invisible to the server-side.
|
overridevirtual |
Connects to a slot.
Every signal can be connected to a slot which does not take any arguments (and may thus ignore signal arguments).
Reimplemented from Wt::EventSignalBase.
const std::string Wt::JSignal< A >::createCall | ( | std::initializer_list< std::string > | args | ) | const |
Returns a JavaScript call that triggers the signal.
This is:
When the signal was constructed with collectSlotJavaScript
== true
, the inline JavaScript from slots defined as JavaScript or from learned stateless slots, or directly connected to the signal, is included as well.
const std::string Wt::JSignal< A >::createEventCall | ( | const std::string & | jsObject, |
const std::string & | jsEvent, | ||
std::initializer_list< std::string > | args | ||
) | const |
Returns a JavaScript call that triggers the signal, passing the original event too.
Similar to createCall(), the following JavaScript is returned:
In addition to information identifying the signal (element
and name
) and the arguments, also information on the original JavaScript event is transferred. In this way, you can propagate the corresponding event class (WMouseEvent, WKeyEvent or WTouchEvent) as an additional last argument in the slot.
void Wt::JSignal< A >::emit | ( | A... | args | ) | const |
Emit the signal.
The arguments must exactly match the arguments of the target function.
This will cause all connected slots to be triggered, with the given arguments.
void Wt::JSignal< A >::operator() | ( | A... | args | ) | const |