# Data Events

A dataset listens for changes to elements of their queries using [data events](/wirebootstrap/data-events/data-events.md).  For example, if a data set finds a change to a query field with the same name sent out in a data event, its able to change its query, re-execute it in the data service, and update its [data promises](/wirebootstrap/reference/wire.data/wire.data.datapromise.md) so UI components and other consumers can react to the change.

## Select Field List

In the following example, any data event sent out for the field `UserName` by `dsFilter` will be picked up by `dsTable` as a change.  This is because the `UserName` field is in both select field lists.  A change to `UserName` will result in `dsTable` re-executing its query based on the change.  `dsTable`  will then update its promises with new data.

<pre class="language-typescript"><code class="lang-typescript"><strong>...
</strong>
const dsFilter = new wire.data.DataSet({
    Events: false,
    Source: source1,
    Query: wire.data.select("UserName")
        .from("Users")
});

const dsTable = new wire.data.DataSet({
    Source: source2
    Query: wire.data.select("UserName", "FullName")
        .from("Employees")      
});
</code></pre>

{% hint style="info" %}
Note, `dsFilter.Events` is set to `false` as most filters don't need to listen for changes.

Also, each dataset uses a different data source.  They don't have to be the same.
{% endhint %}

## Filter Field List

In the example below, a data event for `UserName` will also be picked up.  However, in this instance, its because `UserName` is part of a filter rather than the selected field list.

```javascript
...

const dsFilter = new wire.data.DataSet({
    Events: false,
    Source: source1,
    Query: wire.data.select("UserName")
        .from("Users")        
});

const dsTable = new wire.data.DataSet({
    Source: source2
    Query: wire.data.select()
        .from("Employees")  
        .where().eq("UserName","asmith");    
});
```

## Event Names

Consider the following two datasets on the same page bound to two different UI components. Both the entity and field names are not the same however the intent is that data in `dsFilter` be used to change the query in `dsTable`.

However, sometimes its not possible to set up two queries inside datasets to share field changes by name because their names are different in the respective data services. &#x20;

Fields from different database tables or entirely different data services can be mapped to be recognized as the same by the [data event manager](/wirebootstrap/data-events/data-events.md#data-event-manager) using the field or parameter alias `EventName`.

In the following example, the `field` method is used to select the `UserName` field in `dsTable` in order that meta data like `eventName` be set on the field.  An event name `UserName` is set on the `User` field in the `dsTable` query in order for it to be mapped to the field `UserName` in `dsFilter` for the purposes of picking up changes via data events.

```typescript
...

const dsFilter = new wire.data.DataSet({
    Events: false,
    Source: source1,
    Query: wire.data.select("UserName")
        .from("Users")
});

const dsTable = new wire.data.DataSet({
    Source: source2
    Query: wire.data.select("FullName")
        .field("User").eventName("UserName")
        .from("Employees")      
});
```

## Event Fields

Sometimes it is required that a dataset listen for changes to a field that does not participate in the query.  Here, data events for the field should update the query but should not be part of what gets executed against the data source or returned in the results.

In the example below, a data event for the `UserName` field from `dsFilter` will change the query in `dsTable` becaue `dsTable` specified `UserName` as an event field.  `UserName` however is not part of the query in `dsTable`.

```javascript
...

const dsFilter = new wire.data.DataSet({
    Events: false,
    Source: source1,
    Query: wire.data.select("UserName")
        .from("Users")
});

const dsTable = new wire.data.DataSet({
    Source: source2
    Query: wire.data.select("FullName")
        .eventField("UserName")
        .from("Employees")      
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wirebootstrap.com/wirebootstrap/dataevents.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
