Sys.Observer Class
Adds update and management functionality to target objects such as arrays, DOM elements, and objects.
Namespace: Sys
Inherits: None
Sys.Observer.addPropertyChanged(targetObject, myHandler);
Members
Name |
Description |
---|---|
Adds an item to the collection in an observable manner. |
|
Adds an event handler to the target. |
|
Adds an observable event handler to the target. |
|
Adds a propertyChanged event handler to the target. |
|
Adds items to the collection in an observable manner. |
|
Begins the process of updating the target object. |
|
Clears the array of its elements in an observable manner. |
|
Ends the process of updating the target object. |
|
Inserts an item at the specified index in an observable manner. |
|
Indicates that the target is being updated. |
|
Makes an object directly observable by adding observable methods to it. |
|
Raises the collectionChanged event. |
|
Raises an observable event on the target. |
|
Raises a propertyChanged notification event. |
|
Removes the first occurrence of an item from the array in an observable manner. |
|
Removes the item at the specified index from the array in an observable manner. |
|
Removes a collectionChanged event handler from the target. |
|
Removes a propertyChanged event handler from the target. |
|
Removes an observable event handler from the target. |
|
Sets a property or field on the target in an observable manner. |
|
Gets a Boolean value that indicates whether the target is updating. |
Remarks
The Sys.Observer class is based on the Observer pattern. The Sys.Observer class maintains a list of interested dependents (observers) in a separate object (the subject). All methods that are contained in the Sys.Observer class are static.
In order to be used with the Sys.Observer class, an object must be an object, array, or DOM element.
Example
The following example shows how to declare an Observer object and bind it to a DataView object.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>Linked DataViews</title>
<link href="styles/list.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"
src="../MicrosoftAjax/MicrosoftAjax.debug.js"></script>
<script type="text/javascript"
src="../MicrosoftAjax/MicrosoftAjaxTemplates.debug.js"></script>
<script type="text/javascript">
var imageData = [
{ Name: "Crashing water", Description: "A splash of waves captured." },
{ Name: "Dazed", Description: "Mid-day heat?" },
{ Name: "Close Zoom on Giraffe", Description: "Closeup of a Giraffe at Wild Animal Park." },
{ Name: "Pier", Description: "A pier in Morro Bay." },
{ Name: "Seagull reflections", Description: "Seagulls at peace." },
{ Name: "Spain", Description: "In Balboa Park, in downtown San Diego." },
{ Name: "Sumatran Tiger", Description: "Restful." }
];
// Make the data collection observable.
Sys.Observer.makeObservable(imageData);
function moveUp(sender) {
var nameList = $find("names");
// Find the DataView item that the button was in.
var item = nameList.findContext(sender);
var index = item.index;
var dataItem = item.dataItem;
var newIndex = index > 0 ? index - 1 : imageData.length;
// Move data item up one, which invokes an observer.
imageData.beginUpdate();
imageData.remove(dataItem);
imageData.insert(newIndex, dataItem);
imageData.endUpdate();
}
</script>
</head>
<body xmlns:dataview="javascript:Sys.UI.DataView" xmlns:sys="javascript:Sys">
<div class="title">Names:</div>
<ul class="sys-template list" id="names"
sys:attach="dataview"
dataview:data="{{ imageData }}"
dataview:initialselectedindex="0"
dataview:selecteditemclass="selecteditem"
dataview:sys-key="nameList"
>
<li sys:command="select">
<button onclick = "moveUp(this)">↑</button>
<span>{{ Name }}</span>
</li>
</ul>
<div class="title">Descriptions:</div>
<ul class="sys-template list"
sys:attach="dataview"
dataview:data="{{ imageData }}"
dataview:selecteditemclass="selecteditem"
dataview:selectedindex="{binding selectedIndex, source=nameList}"
>
<li sys:command="select">{{ Description }}</li>
</ul>
</body>
</html>