Wt  4.0.0
Public Member Functions | List of all members
Wt::JSignal< A > Class Template Reference

A signal to relay JavaScript to C++ calls. More...

#include <Wt/WJavaScript.h>

Inheritance diagram for Wt::JSignal< A >:
Inheritance graph
[legend]

Public Member Functions

 JSignal (WObject *object, const std::string &name, bool collectSlotJavaScript=false)
 Construct a signal for the given object, and name. More...
 
 ~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. More...
 
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. More...
 
virtual bool isConnected () const override
 Returns whether the signal is connected to at least one slot.
 
template<class F >
Wt::Signals::connection connect (const F &function)
 Connect to a function. More...
 
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. More...
 
void connect (JSlot &slot)
 Connect a slot that is specified as JavaScript only. More...
 
void emit (A...args) const
 Emit the signal. More...
 
void operator() (A...args) const
 Emit the signal. More...
 
virtual Wt::Signals::connection connect (WObject *target, void(WObject::*method)()) override
 Connects to a slot. More...
 
- Public Member Functions inherited from Wt::EventSignalBase
const char * name () const
 Returns the event name. More...
 
void disconnect (JSlot &slot)
 Disconnects a JSlot.
 
void preventDefaultAction (bool prevent=true)
 Prevents the default browser action. More...
 
bool defaultActionPrevented () const
 Returns whether the default browser action is prevented. More...
 
void preventPropagation (bool prevent=true)
 Prevents event propagation. More...
 
bool propagationPrevented () const
 Returns whether event propagation is prevented. More...
 
- 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. More...
 

Detailed Description

template<typename... A>
class Wt::JSignal< A >

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:

class MyWidget : public WCompositeWidget
{
public:
MyWidget()
: doSome_(this, "doSome")
{
...
}
JSignal<std::string, int>& doSome() { return doSome_; }
private:
JSignal<std::string, int> doSome_;
...
};

The following JavaScript statement will emit the signal for a DOM element element that corresponds to a widget of class MyWidget:

Wt.emit(element, 'dosome', 'foo', 42);

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:

doSome_.createCall("'foo'", "42");

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:

JSignal<std::string, int, WMouseEvent> doSome();

Then, the following would be a suitable JavaScript call:

Wt.emit(Wt, {name: 'dosome', event: event, eventObject: object}, 'foo', 42);

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.

See also
WWidget::jsRef(), WObject::id()

Constructor & Destructor Documentation

template<typename... A>
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.

See also
stateless slot learning

Member Function Documentation

template<typename... A>
template<class F >
Wt::Signals::connection Wt::JSignal< A >::connect ( const 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).

template<typename... A>
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):

function(object, 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.

template<typename... A>
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.

template<typename... A>
Wt::Signals::connection Wt::JSignal< A >::connect ( WObject target,
void(WObject::*)()  method 
)
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).

Implements Wt::SignalBase.

template<typename... A>
const std::string Wt::JSignal< A >::createCall ( std::initializer_list< std::string >  args) const

Returns a JavaScript call that triggers the signal.

This is:

Wt.emit([element], [name], arg1, ...);

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.

Note
The method only takes into account JavaScript from slot connections that have been connected so far, and any subsequent connections are ignored.
See also
createEventCall()
template<typename... A>
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:

Wt.emit([element], { name: [name], eventObject: [jsObject], event: [jsEvent]},
arg1, ...);

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.

Note
The method only takes into account JavaScript from slot connections that have been connected so far, and any subsequent connections are ignored.
template<typename... A>
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.

template<typename... A>
void Wt::JSignal< A >::operator() ( A...  args) const

Emit the signal.

This is equivalent to emit().

See also
emit

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11