Options
All
  • Public
  • Public/Protected
  • All
Menu

An Observable wraps a value that is modified using a call to set() and fetched with a call to get(). An Observable has an internal binding event that is raised when set() is called.

This mechanism allows interested parties to receive notifications when a particular value changes, and this pattern is used by the HTML5 framework to allow construction of data-bound applications that respond automatically to data as it changes.

docs-hide-from-nav

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • Constructs a new instance of the Observable, wrapping the given value.

    var property = new Observable("hello");

    Parameters

    • Optional value: T

      The initial value of the Observable.

    Returns Observable

Properties

Private bindingEvent

bindingEvent: GeocortexEvent

Methods

bind

  • bind(scope: any, handler: function): string
  • Binds an event handler to the binding event of this Observable. The event handler will be executed when the value of the Observable changes via a call to set(). A string token is returned that can be used to unbind the handler from the event.

    var property = new Observable();
    property.bind(this, function(value) {
        alert(value);
    });
    
    // Alerts "hello world"
    property.set("hello world");

    Parameters

    • scope: any

      The value of 'this' to bind the handler to.

    • handler: function

      The handler function to execute when the value changes.

        • (value: T): void
        • Parameters

          • value: T

          Returns void

    Returns string

equals

  • Compares the value of this Observable to the value of another object. If the other object is an Observable, its value will be used in the comparison.

    var a = new Observable("hello");
    var b = new Observable("hello");
    
    // Alerts "true"
    alert(a.equals(b));

    Parameters

    • other: T | Observable<T>

      The other raw value or Observable to compare.

    Returns boolean

get

  • get(): T
  • Returns the value of the Observable. Get should always be used to fetch the value of an Observable.

    var property = new Observable("hello");
    
    // Alerts "hello"
    alert(property.get());

    Returns T

once

  • once(scope: any, handler: Function): string
  • Binds a one-time event handler to the Observable, unbinding it the next time the Observable fires.

    var property = new Observable();
    var token = property.once(this, function(value) {
        alert(value);
    });
    
    // Alerts "hello world"
    property.set("hello world");
    
    // Does not alert anything
    property.set("hello?");

    Parameters

    • scope: any

      The value of 'this' inside of the handler.

    • handler: Function

      The one-time handler to execute.

    Returns string

pulse

  • pulse(): void
  • Sets the Observable to its current value and fires the binding event.

    var property1 = new Observable("hello");
    property1.bind(this, function (value) {
        alert(value);
    });
    
    // Alerts "hello".
    property1.pulse();

    Returns void

removeSync

  • removeSync(): void
  • Removes the event subscriptions that were created when sync() or syncTransform() was called. This effectively unbinds the synchronization that exists between this Observable and another Observable.

    Returns void

set

  • set(value: T): void
  • Sets the value of the Observable, raising the binding event and notifying any subscribers of the new value. Set() should always be used to modify Observable values.

    var property = new Observable("hello");
    
    // Updates the value and fires the binding event.
    property.set("hello world");
    
    // Alerts "hello world"
    alert(property.get());

    Parameters

    • value: T

      The new value to set the Observable to.

    Returns void

sync

  • Synchronizes this Observable to another Observable. When the other Observable changes, this Observable will be set to the same value, and the binding event raised. The value of this Observable will be set to the value of the other Observable upon synchronization.

    var property1 = new Observable("hello");
    var property2 = new Observable();
    
    property2.sync(property1);
    
    // Alerts "hello"
    alert(property2.get());
    
    property1.set("bye");
    
    // Alerts "bye"
    alert(property2.get());

    Parameters

    Returns void

syncTransform

  • syncTransform<U>(source: Observable<U>, transformation: function): void
  • Works the same as sync, but calls a transformation function every time the other Observable changes.

    var property1 = new Observable("hello");
    var property2 = new Observable();
    
    property2.syncTransform(property1, function (value) {
         return value.toUpperCase());
    });
    
    // Alerts "HELLO"
    alert(property2.get());
    
    property1.set("bye");
    
    // Alerts "BYE"
    alert(property2.get());

    Type parameters

    • U

    Parameters

    • source: Observable<U>

      The Observable to sync to.

    • transformation: function

      The function that performs the desired transformation.

        • (value: U): T
        • Parameters

          • value: U

          Returns T

    Returns void

unbind

  • unbind(token: string): boolean
  • Removes an event handler previously attached via bind(), given the token that bind() returned.

    var property = new Observable();
    var token = property.bind(this, function(value) {
        alert(value);
    });
    
    // Alerts "hello world"
    property.set("hello world");
    property.unbind(token);
    
    // Does not alert anything
    property.set("hello?");

    Parameters

    • token: string

      The token previously returned from a call to bind().

    Returns boolean

Static Private makeBindableProxy

  • makeBindableProxy(obj: any): any
  • Parameters

    • obj: any

    Returns any