Wt  3.3.9
Public Member Functions | List of all members
Wt::JSlot Class Reference

A slot that is only implemented in client side JavaScript code. More...

#include <Wt/WJavaScript>

Public Member Functions

 JSlot (WWidget *parent=0)
 Constructs a JavaScript-only slot within the parent scope. More...
 
 JSlot (const std::string &javaScript, WWidget *parent=0)
 Constructs a JavaScript-only slot and sets the JavaScript code. More...
 
 JSlot (int nbArgs, WWidget *parent)
 Constructs a JavaScript-only slot and set the number of arguments. More...
 
 JSlot (const std::string &javaScript, int nbArgs, WWidget *parent=0)
 Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments. More...
 
 ~JSlot ()
 Destructor.
 
void setJavaScript (const std::string &javaScript, int nbArgs=0)
 Set or modify the JavaScript code associated with the slot. More...
 
void exec (const std::string &object="null", const std::string &event="null", const std::string &arg1="null", const std::string &arg2="null", const std::string &arg3="null", const std::string &arg4="null", const std::string &arg5="null", const std::string &arg6="null")
 Executes the JavaScript code. More...
 
std::string execJs (const std::string &object="null", const std::string &event="null", const std::string &arg1="null", const std::string &arg2="null", const std::string &arg3="null", const std::string &arg4="null", const std::string &arg5="null", const std::string &arg6="null")
 Returns a JavaScript statement that executes the slot. More...
 
int getNbArgs ()
 Returns the number of extra arguments this JSlot takes.
 

Detailed Description

A slot that is only implemented in client side JavaScript code.

This class provides a hook for adding your own JavaScript to respond to events.

Carefully consider the use of this. Not only is writing cross-browser JavaScript hard and tedious, but one must also be aware of possible security problems (see further), and of course, the event handling will not be available when JavaScript is disabled or not present at all.

If you wish to add client side event handling, with automatic fall-back to server-side event handling and without writing JavaScript code with the associated risks and problems, consider using stateless slot implementations instead (see WObject::implementStateless())

For some purposes, stateless slot implementations are not sufficient, since they do not allow state inspection. At the same time, the non-availability in case of disabled JavaScript may also be fine for some non-essential functionality (see for example the WSuggestionPopup widget), or when you simply do not care. For these situations a JSlot can be used to add client-side event handling.

The JavaScript code may be set (or changed) using the setJavaScript() method which takes a string that implements a JavaScript function with the following signature:

function(sender, event) {
// handle the event, and sender is a reference to the DOM element
// which captured the event (and holds the signal). Therefore it
// equivalent to the sender for a normal %Wt slot.
// You can prevent the default action using:
var fixed = jQuery.event.fix(event);
fixed.preventDefault();
fixed.stopPropagation();
}

In the JavaScript code, you may use WWidget::jsRef() to obtain the DOM element corresponding to any WWidget, or WWidget::id() to obtain the DOM id. In addition you may trigger server-side events using the JavaScript WtSignalEmit function (see JSignal documentation).

A JSlot can take up to six extra arguments. This is so that a JSignal can pass its arguments directly on to a JSlot, without communicating with the server.

That's how far we can help you. For the rest you are left to yourself, buggy browsers and quirky JavaScript (http://www.quirksmode.org/ was a reliable companion to me) – good luck.

Note that the slot object needs to live as long as you want the JavaScript to be executed by connected signals: when the slot is destroyed, the connection is destroyed just as with other signal/slot connections where the target object is deleted. This means that it is (almost?) always a bad idea to declare a JSlot on the stack.

Constructor & Destructor Documentation

Wt::JSlot::JSlot ( WWidget parent = 0)

Constructs a JavaScript-only slot within the parent scope.

The JavaScript code block will reside within the scope of the given widget. By picking a long-lived parent, one may reuse a single block of JavaScript code for multiple widgets.

When parent = 0, then the JavaScript will be inlined in each caller (possibly replicating the same JavaScript).

The slot will have no extra arguments.

Wt::JSlot::JSlot ( const std::string &  javaScript,
WWidget parent = 0 
)

Constructs a JavaScript-only slot and sets the JavaScript code.

The slot will have no extra arguments.

See also
JSlot(WWidget *), setJavaScript()
Wt::JSlot::JSlot ( int  nbArgs,
WWidget parent 
)

Constructs a JavaScript-only slot and set the number of arguments.

See also
JSlot(WWidget *), setJavaScript()
Wt::JSlot::JSlot ( const std::string &  javaScript,
int  nbArgs,
WWidget parent = 0 
)

Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments.

See also
JSlot(WWidget *), setJavaScript()

Member Function Documentation

void Wt::JSlot::exec ( const std::string &  object = "null",
const std::string &  event = "null",
const std::string &  arg1 = "null",
const std::string &  arg2 = "null",
const std::string &  arg3 = "null",
const std::string &  arg4 = "null",
const std::string &  arg5 = "null",
const std::string &  arg6 = "null" 
)

Executes the JavaScript code.

This executes the JavaScript code in the same way as when triggered by a EventSignal. This function returns immediately, and execution of the JavaScript code is deferred until after the event handling.

The first two arguments are the "object, event" arguments of the JavaScript event callback function.

See also
setJavaScript()
std::string Wt::JSlot::execJs ( const std::string &  object = "null",
const std::string &  event = "null",
const std::string &  arg1 = "null",
const std::string &  arg2 = "null",
const std::string &  arg3 = "null",
const std::string &  arg4 = "null",
const std::string &  arg5 = "null",
const std::string &  arg6 = "null" 
)

Returns a JavaScript statement that executes the slot.

This returns the JavaScript code to execute the slot.

The arguments are the "object, event" arguments of the JavaScript event callback function.

See also
exec()
void Wt::JSlot::setJavaScript ( const std::string &  javaScript,
int  nbArgs = 0 
)

Set or modify the JavaScript code associated with the slot.

When the slot is triggered, the corresponding function defined by javaScript is executed.

The JavaScript function takes at least two parameters and thus should look like:

function(obj, event) {
// ...
}

The first parameter obj is a reference to the DOM element that generates the event. The event refers to the JavaScript event object.

The JavaScript function can take up to six extra arguments, which is to be configured using the nbArgs parameter.

function(obj, event, a1, a2, a3, a4, a5, a6) {
// ...
}

If this JSlot is connected to a JSignal, that JSignal's arguments will be passed on to the JSlot.

See also
WWidget::jsRef()

Generated on Tue Nov 21 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11