Ember.StateManager Class

StateManager is part of Ember's implementation of a finite state machine. A StateManager instance manages a number of properties that are instances of Ember.State, tracks the current active state, and triggers callbacks when states have changed.

Defining States

The states of StateManager can be declared in one of two ways. First, you can define a states property that contains all the states:

1
2
3
4
5
6
7
8
9
10
11
12
managerA = Ember.StateManager.create({
  states: {
    stateOne: Ember.State.create(),
    stateTwo: Ember.State.create()
  }
})

managerA.get('states')
// {
//   stateOne: Ember.State.create(),
//   stateTwo: Ember.State.create()
// }

You can also add instances of Ember.State (or an Ember.State subclass) directly as properties of a StateManager. These states will be collected into the states property for you.

1
2
3
4
5
6
7
8
9
10
managerA = Ember.StateManager.create({
  stateOne: Ember.State.create(),
  stateTwo: Ember.State.create()
})

managerA.get('states')
// {
//   stateOne: Ember.State.create(),
//   stateTwo: Ember.State.create()
// }

The Initial State

When created a StateManager instance will immediately enter into the state defined as its start property or the state referenced by name in its initialState property:

1
2
3
4
5
6
7
8
9
10
11
12
managerA = Ember.StateManager.create({
  start: Ember.State.create({})
})

managerA.get('currentState.name') // 'start'

managerB = Ember.StateManager.create({
  initialState: 'beginHere',
  beginHere: Ember.State.create({})
})

managerB.get('currentState.name') // 'beginHere'

Because it is a property you may also provide a computed function if you wish to derive an initialState programmatically:

1
2
3
4
5
6
7
8
9
10
11
managerC = Ember.StateManager.create({
  initialState: function(){
    if (someLogic) {
      return 'active';
    } else {
      return 'passive';
    }
  }.property(),
  active: Ember.State.create({}),
  passive: Ember.State.create({})
})

Moving Between States

A StateManager can have any number of Ember.State objects as properties and can have a single one of these states as its current state.

Calling transitionTo transitions between states:

1
2
3
4
5
6
7
8
9
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown',
  poweredDown: Ember.State.create({}),
  poweredUp: Ember.State.create({})
})

robotManager.get('currentState.name') // 'poweredDown'
robotManager.transitionTo('poweredUp')
robotManager.get('currentState.name') // 'poweredUp'

Before transitioning into a new state the existing currentState will have its exit method called with the StateManager instance as its first argument and an object representing the transition as its second argument.

After transitioning into a new state the new currentState will have its enter method called with the StateManager instance as its first argument and an object representing the transition as its second argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown',
  poweredDown: Ember.State.create({
    exit: function(stateManager){
      console.log("exiting the poweredDown state")
    }
  }),
  poweredUp: Ember.State.create({
    enter: function(stateManager){
      console.log("entering the poweredUp state. Destroy all humans.")
    }
  })
})

robotManager.get('currentState.name') // 'poweredDown'
robotManager.transitionTo('poweredUp')

// will log
// 'exiting the poweredDown state'
// 'entering the poweredUp state. Destroy all humans.'

Once a StateManager is already in a state, subsequent attempts to enter that state will not trigger enter or exit method calls. Attempts to transition into a state that the manager does not have will result in no changes in the StateManager's current state:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown',
  poweredDown: Ember.State.create({
    exit: function(stateManager){
      console.log("exiting the poweredDown state")
    }
  }),
  poweredUp: Ember.State.create({
    enter: function(stateManager){
      console.log("entering the poweredUp state. Destroy all humans.")
    }
  })
})

robotManager.get('currentState.name') // 'poweredDown'
robotManager.transitionTo('poweredUp')
// will log
// 'exiting the poweredDown state'
// 'entering the poweredUp state. Destroy all humans.'
robotManager.transitionTo('poweredUp') // no logging, no state change

robotManager.transitionTo('someUnknownState') // silently fails
robotManager.get('currentState.name') // 'poweredUp'

Each state property may itself contain properties that are instances of Ember.State. The StateManager can transition to specific sub-states in a series of transitionTo method calls or via a single transitionTo with the full path to the specific state. The StateManager will also keep track of the full path to its currentState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown',
  poweredDown: Ember.State.create({
    charging: Ember.State.create(),
    charged: Ember.State.create()
  }),
  poweredUp: Ember.State.create({
    mobile: Ember.State.create(),
    stationary: Ember.State.create()
  })
})

robotManager.get('currentState.name') // 'poweredDown'

robotManager.transitionTo('poweredUp')
robotManager.get('currentState.name') // 'poweredUp'

robotManager.transitionTo('mobile')
robotManager.get('currentState.name') // 'mobile'

// transition via a state path
robotManager.transitionTo('poweredDown.charging')
robotManager.get('currentState.name') // 'charging'

robotManager.get('currentState.path') // 'poweredDown.charging'

Enter transition methods will be called for each state and nested child state in their hierarchical order. Exit methods will be called for each state and its nested states in reverse hierarchical order.

Exit transitions for a parent state are not called when entering into one of its child states, only when transitioning to a new section of possible states in the hierarchy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown',
  poweredDown: Ember.State.create({
    enter: function(){},
    exit: function(){
      console.log("exited poweredDown state")
    },
    charging: Ember.State.create({
      enter: function(){},
      exit: function(){}
    }),
    charged: Ember.State.create({
      enter: function(){
        console.log("entered charged state")
      },
      exit: function(){
        console.log("exited charged state")
      }
    })
  }),
  poweredUp: Ember.State.create({
    enter: function(){
      console.log("entered poweredUp state")
    },
    exit: function(){},
    mobile: Ember.State.create({
      enter: function(){
        console.log("entered mobile state")
      },
      exit: function(){}
    }),
    stationary: Ember.State.create({
      enter: function(){},
      exit: function(){}
    })
  })
})


robotManager.get('currentState.path') // 'poweredDown'
robotManager.transitionTo('charged')
// logs 'entered charged state'
// but does *not* log  'exited poweredDown state'
robotManager.get('currentState.name') // 'charged

robotManager.transitionTo('poweredUp.mobile')
// logs
// 'exited charged state'
// 'exited poweredDown state'
// 'entered poweredUp state'
// 'entered mobile state'

During development you can set a StateManager's enableLogging property to true to receive console messages of state transitions.

1
2
3
robotManager = Ember.StateManager.create({
  enableLogging: true
})

Managing currentState with Actions

To control which transitions are possible for a given state, and appropriately handle external events, the StateManager can receive and route action messages to its states via the send method. Calling to send with an action name will begin searching for a method with the same name starting at the current state and moving up through the parent states in a state hierarchy until an appropriate method is found or the StateManager instance itself is reached.

If an appropriately named method is found it will be called with the state manager as the first argument and an optional context object as the second argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
managerA = Ember.StateManager.create({
  initialState: 'stateOne.substateOne.subsubstateOne',
  stateOne: Ember.State.create({
    substateOne: Ember.State.create({
      anAction: function(manager, context){
        console.log("an action was called")
      },
      subsubstateOne: Ember.State.create({})
    })
  })
})

managerA.get('currentState.name') // 'subsubstateOne'
managerA.send('anAction')
// 'stateOne.substateOne.subsubstateOne' has no anAction method
// so the 'anAction' method of 'stateOne.substateOne' is called
// and logs "an action was called"
// with managerA as the first argument
// and no second argument

someObject = {}
managerA.send('anAction', someObject)
// the 'anAction' method of 'stateOne.substateOne' is called again
// with managerA as the first argument and
// someObject as the second argument.

If the StateManager attempts to send an action but does not find an appropriately named method in the current state or while moving upwards through the state hierarchy, it will repeat the process looking for a unhandledEvent method. If an unhandledEvent method is found, it will be called with the original event name as the second argument. If an unhandledEvent method is not found, the StateManager will throw a new Ember.Error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
managerB = Ember.StateManager.create({
  initialState: 'stateOne.substateOne.subsubstateOne',
  stateOne: Ember.State.create({
    substateOne: Ember.State.create({
      subsubstateOne: Ember.State.create({}),
      unhandledEvent: function(manager, eventName, context) {
        console.log("got an unhandledEvent with name " + eventName);
      }
    })
  })
})

managerB.get('currentState.name') // 'subsubstateOne'
managerB.send('anAction')
// neither `stateOne.substateOne.subsubstateOne` nor any of it's
// parent states have a handler for `anAction`. `subsubstateOne`
// also does not have a `unhandledEvent` method, but its parent
// state, `substateOne`, does, and it gets fired. It will log
// "got an unhandledEvent with name anAction"

Action detection only moves upwards through the state hierarchy from the current state. It does not search in other portions of the hierarchy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
managerC = Ember.StateManager.create({
  initialState: 'stateOne.substateOne.subsubstateOne',
  stateOne: Ember.State.create({
    substateOne: Ember.State.create({
      subsubstateOne: Ember.State.create({})
    })
  }),
  stateTwo: Ember.State.create({
   anAction: function(manager, context){
     // will not be called below because it is
     // not a parent of the current state
   }
  })
})

managerC.get('currentState.name') // 'subsubstateOne'
managerC.send('anAction')
// Error: <Ember.StateManager:ember132> could not
// respond to event anAction in state stateOne.substateOne.subsubstateOne.

Inside of an action method the given state should delegate transitionTo calls on its StateManager.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
robotManager = Ember.StateManager.create({
  initialState: 'poweredDown.charging',
  poweredDown: Ember.State.create({
    charging: Ember.State.create({
      chargeComplete: function(manager, context){
        manager.transitionTo('charged')
      }
    }),
    charged: Ember.State.create({
      boot: function(manager, context){
        manager.transitionTo('poweredUp')
      }
    })
  }),
  poweredUp: Ember.State.create({
    beginExtermination: function(manager, context){
      manager.transitionTo('rampaging')
    },
    rampaging: Ember.State.create()
  })
})

robotManager.get('currentState.name') // 'charging'
robotManager.send('boot') // throws error, no boot action
                          // in current hierarchy
robotManager.get('currentState.name') // remains 'charging'

robotManager.send('beginExtermination') // throws error, no beginExtermination
                                        // action in current hierarchy
robotManager.get('currentState.name')   // remains 'charging'

robotManager.send('chargeComplete')
robotManager.get('currentState.name')   // 'charged'

robotManager.send('boot')
robotManager.get('currentState.name')   // 'poweredUp'

robotManager.send('beginExtermination', allHumans)
robotManager.get('currentState.name')   // 'rampaging'

Transition actions can also be created using the transitionTo method of the Ember.State class. The following example StateManagers are equivalent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
aManager = Ember.StateManager.create({
  stateOne: Ember.State.create({
    changeToStateTwo: Ember.State.transitionTo('stateTwo')
  }),
  stateTwo: Ember.State.create({})
})

bManager = Ember.StateManager.create({
  stateOne: Ember.State.create({
    changeToStateTwo: function(manager, context){
      manager.transitionTo('stateTwo', context)
    }
  }),
  stateTwo: Ember.State.create({})
})
Show:

_scheduledDestroy

private

addObserver

(key, target, method) Ember.Object

Adds an observer on a property.

This is the core method used to register an observer for a property.

Once you call this method, anytime the key's value is set, your observer will be notified. Note that the observers are triggered anytime the value is set, regardless of whether it has actually changed. Your observer should be prepared to handle that.

You can also pass an optional context parameter to this method. The context will be passed to your observer method whenever it is triggered. Note that if you add the same target/method pair on a key multiple times with different context parameters, your observer will only be called once with the last context you passed.

Observer Methods

Observer methods you pass should generally have the following signature if you do not pass a context parameter:

1
fooDidChange: function(sender, key, value, rev) { };

The sender is the object that changed. The key is the property that changes. The value property is currently reserved and unused. The rev is the last property revision of the object when it changed, which you can use to detect if the key value has really changed or not.

If you pass a context parameter, the context will be passed before the revision like so:

1
fooDidChange: function(sender, key, value, context, rev) { };

Usually you will not need the value, context or revision parameters at the end. In this case, it is common to write observer methods that take only a sender and key value as parameters or, if you aren't interested in any of these values, to write an observer that has no parameters at all.

Parameters:

key String
The key to observer
target Object
The target object to invoke
method String|Function
The method to invoke.

Returns:

Ember.Object
self

apply

(obj)

Parameters:

obj

Returns:

applied object

beginPropertyChanges

Ember.Observable

Begins a grouping of property changes.

You can use this method to group property changes so that notifications will not be sent until the changes are finished. If you plan to make a large number of changes to an object at one time, you should call this method at the beginning of the changes to begin deferring change notifications. When you are done making changes, call endPropertyChanges() to deliver the deferred change notifications and end deferring.

Returns:

Ember.Observable

cacheFor

(keyName) Object

Returns the cached value of a computed property, if it exists. This allows you to inspect the value of a computed property without accidentally invoking it if it is intended to be generated lazily.

Parameters:

keyName String

Returns:

Object
The cached value of the computed property, if any

create

(arguments) static

Creates an instance of a class. Accepts either no arguments, or an object containing values to initialize the newly instantiated object with.

1
2
3
4
5
6
7
8
9
10
11
App.Person = Ember.Object.extend({
  helloWorld: function() {
    alert("Hi, my name is " + this.get('name'));
  }
});

var tom = App.Person.create({
  name: 'Tom Dale'
});

tom.helloWorld(); // alerts "Hi, my name is Tom Dale".

create will call the init function if defined during Ember.AnyObject.extend

If no arguments are passed to create, it will not set values to the new instance during initialization:

1
2
var noName = App.Person.create();
noName.helloWorld(); // alerts undefined

NOTE: For performance reasons, you cannot declare methods or computed properties during create. You should instead declare methods and computed properties when using extend.

Parameters:

arguments

decrementProperty

(keyName, increment) Object

Set the value of a property to the current value minus some amount.

1
2
player.decrementProperty('lives');
orc.decrementProperty('health', 5);

Parameters:

keyName String
The name of the property to decrement
increment Object
The amount to decrement by. Defaults to 1

Returns:

Object
The new property value

destroy

Ember.Object

Destroys an object by setting the isDestroyed flag and removing its metadata, which effectively destroys observers and bindings.

If you try to set a property on a destroyed object, an exception will be raised.

Note that destruction is scheduled for the end of the run loop and does not happen immediately.

Returns:

Ember.Object
receiver

detect

(obj) Boolean

Parameters:

obj

Returns:

Boolean

eachComputedProperty

(callback, binding)

Iterate over each computed property for the class, passing its name and any associated metadata (see metaForProperty) to the callback.

Parameters:

callback Function
binding Object

endPropertyChanges

Ember.Observable

Ends a grouping of property changes.

You can use this method to group property changes so that notifications will not be sent until the changes are finished. If you plan to make a large number of changes to an object at one time, you should call beginPropertyChanges() at the beginning of the changes to defer change notifications. When you are done making changes, call this method to deliver the deferred change notifications and end deferring.

Returns:

Ember.Observable

get

(keyName) Object

Retrieves the value of a property from the object.

This method is usually similar to using object[keyName] or object.keyName, however it supports both computed properties and the unknownProperty handler.

Because get unifies the syntax for accessing all these kinds of properties, it can make many refactorings easier, such as replacing a simple property with a computed property, or vice versa.

Computed Properties

Computed properties are methods defined with the property modifier declared at the end, such as:

1
2
3
fullName: function() {
  return this.getEach('firstName', 'lastName').compact().join(' ');
}.property('firstName', 'lastName')

When you call get on a computed property, the function will be called and the return value will be returned instead of the function itself.

Unknown Properties

Likewise, if you try to call get on a property whose value is undefined, the unknownProperty() method will be called on the object. If this method returns any value other than undefined, it will be returned instead. This allows you to implement "virtual" properties that are not defined upfront.

Parameters:

keyName String
The property to retrieve

Returns:

Object
The property value or undefined.

getPath

(path) Object deprecated

Parameters:

path String
The property path to retrieve

Returns:

Object
The property value or undefined.

getProperties

(list) Hash

To get multiple properties at once, call getProperties with a list of strings or an array:

1
record.getProperties('firstName', 'lastName', 'zipCode');  // { firstName: 'John', lastName: 'Doe', zipCode: '10011' }

is equivalent to:

1
record.getProperties(['firstName', 'lastName', 'zipCode']);  // { firstName: 'John', lastName: 'Doe', zipCode: '10011' }

Parameters:

list String...|Array
of keys to get

Returns:

Hash

getStateByPath

(root, path) Ember.State

Finds a state by its state path.

Example:

1
2
3
4
5
6
7
8
9
manager = Ember.StateManager.create({
  root: Ember.State.create({
    dashboard: Ember.State.create()
  })
});

manager.getStateByPath(manager, "root.dashboard")

// returns the dashboard state

Parameters:

root Ember.State
the state to start searching from
path String
the state path to follow

Returns:

Ember.State
the state at the end of the path

getStatesInPath

(root, path)

A state stores its child states in its states hash. This code takes a path like posts.show and looks up root.states.posts.states.show.

It returns a list of all of the states from the root, which is the list of states to call enter on.

Parameters:

root
path

getWithDefault

(keyName, defaultValue) Object

Retrieves the value of a property, or a default value in the case that the property returns undefined.

1
person.getWithDefault('lastName', 'Doe');

Parameters:

keyName String
The name of the property to retrieve
defaultValue Object
The value to return if the property value is undefined

Returns:

Object
The property value or the defaultValue.

has

(name) Boolean

Checks to see if object has any subscriptions for named event.

Parameters:

name String
The name of the event

Returns:

Boolean
does the object have a subscription for event

hasObserverFor

(key) Boolean

Returns true if the object currently has observers registered for a particular key. You can use this method to potentially defer performing an expensive action until someone begins observing a particular property on the object.

Parameters:

key String
Key to check

Returns:

Boolean

incrementProperty

(keyName, increment) Object

Set the value of a property to the current value plus some amount.

1
2
person.incrementProperty('age');
team.incrementProperty('score', 2);

Parameters:

keyName String
The name of the property to increment
increment Object
The amount to increment by. Defaults to 1

Returns:

Object
The new property value

init

private

metaForProperty

(key)

In some cases, you may want to annotate computed properties with additional metadata about how they function or what values they operate on. For example, computed property functions may close over variables that are then no longer available for introspection.

You can pass a hash of these values to a computed property like this:

1
2
3
4
person: function() {
  var personId = this.get('personId');
  return App.Person.create({ id: personId });
}.property().meta({ type: App.Person })

Once you've done this, you can retrieve the values saved to the computed property from your class like this:

1
MyClass.metaForProperty('person');

This will return the original hash that was passed to meta().

Parameters:

key String
property name

notifyPropertyChange

(keyName) Ember.Observable

Convenience method to call propertyWillChange and propertyDidChange in succession.

Parameters:

keyName String
The property key to be notified about.

Returns:

Ember.Observable

off

(name, target, method)

Cancels subscription for give name, target, and method.

Parameters:

name String
The name of the event
target Object
The target of the subscription
method Function
The function of the subscription

Returns:

this

on

(name, target, method)

Subscribes to a named event with given function.

1
2
3
person.on('didLoad', function() {
  // fired once the person has loaded
});

An optional target can be passed in as the 2nd argument that will be set as the "this" for the callback. This is a good way to give your function access to the object triggering the event. When the target parameter is used the callback becomes the third argument.

Parameters:

name String
The name of the event
target Object
The "this" binding for the callback
method Function
The callback to execute

Returns:

this

one

(name, target, method)

Subscribes a function to a named event and then cancels the subscription after the first time the event is triggered. It is good to use one when you only care about the first time an event has taken place.

This function takes an optional 2nd argument that will become the "this" value for the callback. If this argument is passed then the 3rd argument becomes the function.

Parameters:

name String
The name of the event
target Object
The "this" binding for the callback
method Function
The callback to execute

Returns:

this

propertyDidChange

(keyName) Ember.Observable

Notify the observer system that a property has just changed.

Sometimes you need to change a value directly or indirectly without actually calling get() or set() on it. In this case, you can use this method and propertyWillChange() instead. Calling these two methods together will notify all observers that the property has potentially changed value.

Note that you must always call propertyWillChange and propertyDidChange as a pair. If you do not, it may get the property change groups out of order and cause notifications to be delivered more often than you would like.

Parameters:

keyName String
The property key that has just changed.

Returns:

Ember.Observable

propertyWillChange

(keyName) Ember.Observable

Notify the observer system that a property is about to change.

Sometimes you need to change a value directly or indirectly without actually calling get() or set() on it. In this case, you can use this method and propertyDidChange() instead. Calling these two methods together will notify all observers that the property has potentially changed value.

Note that you must always call propertyWillChange and propertyDidChange as a pair. If you do not, it may get the property change groups out of order and cause notifications to be delivered more often than you would like.

Parameters:

keyName String
The property key that is about to change.

Returns:

Ember.Observable

removeObserver

(key, target, method) Ember.Observable

Remove an observer you have previously registered on this object. Pass the same key, target, and method you passed to addObserver() and your target will no longer receive notifications.

Parameters:

key String
The key to observer
target Object
The target object to invoke
method String|Function
The method to invoke.

Returns:

Ember.Observable
receiver

reopen

(arguments)

Parameters:

arguments

set

(keyName, value) Ember.Observable

Sets the provided key or path to the value.

This method is generally very similar to calling object[key] = value or object.key = value, except that it provides support for computed properties, the unknownProperty() method and property observers.

Computed Properties

If you try to set a value on a key that has a computed property handler defined (see the get() method for an example), then set() will call that method, passing both the value and key instead of simply changing the value itself. This is useful for those times when you need to implement a property that is composed of one or more member properties.

Unknown Properties

If you try to set a value on a key that is undefined in the target object, then the unknownProperty() handler will be called instead. This gives you an opportunity to implement complex "virtual" properties that are not predefined on the object. If unknownProperty() returns undefined, then set() will simply set the value on the object.

Property Observers

In addition to changing the property, set() will also register a property change with the object. Unless you have placed this call inside of a beginPropertyChanges() and endPropertyChanges(), any "local" observers (i.e. observer methods declared on the same object), will be called immediately. Any "remote" observers (i.e. observer methods declared on another object) will be placed in a queue and called at a later time in a coalesced manner.

Chaining

In addition to property changes, set() returns the value of the object itself so you can do chaining like this:

1
record.set('firstName', 'Charles').set('lastName', 'Jolley');

Parameters:

keyName String
The property to set
value Object
The value to set or `null`.

Returns:

Ember.Observable

setPath

(path, value) Ember.Observable deprecated

Parameters:

path String
The path to the property that will be set
value Object
The value to set or `null`.

Returns:

Ember.Observable

setProperties

(hash) Ember.Observable

To set multiple properties at once, call setProperties with a Hash:

1
record.setProperties({ firstName: 'Charles', lastName: 'Jolley' });

Parameters:

hash Hash
the hash of keys and values to set

Returns:

Ember.Observable

toString

String

Returns a string representation which attempts to provide more information than Javascript's toString typically does, in a generic way for all Ember objects.

1
2
3
App.Person = Em.Object.extend()
person = App.Person.create()
person.toString() //=> "<App.Person:ember1024>"

If the object's class is not defined on an Ember namespace, it will indicate it is a subclass of the registered superclass:

1
2
3
Student = App.Person.extend()
student = Student.create()
student.toString() //=> "<(subclass of App.Person):ember1025>"

If the method toStringExtension is defined, its return value will be included in the output.

1
2
3
4
5
6
7
App.Teacher = App.Person.extend({
  toStringExtension: function(){
    return this.get('fullName');
  }
});
teacher = App.Teacher.create()
teacher.toString(); //=> "<App.Teacher:ember1026:Tom Dale>"

Returns:

String
string representation

toggleProperty

(keyName) Object

Set the value of a boolean property to the opposite of it's current value.

1
starship.toggleProperty('warpDriveEnaged');

Parameters:

keyName String
The name of the property to toggle

Returns:

Object
The new property value

transitionTo

(target) static

Creates an action function for transitioning to the named state while preserving context.

The following example StateManagers are equivalent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
aManager = Ember.StateManager.create({
  stateOne: Ember.State.create({
    changeToStateTwo: Ember.State.transitionTo('stateTwo')
  }),
  stateTwo: Ember.State.create({})
})

bManager = Ember.StateManager.create({
  stateOne: Ember.State.create({
    changeToStateTwo: function(manager, context){
      manager.transitionTo('stateTwo', context)
    }
  }),
  stateTwo: Ember.State.create({})
})

Parameters:

target String

trigger

(name) private
Inherited from Ember.Evented but overwritten in packages/ember-states/lib/state.js:52

Parameters:

name
Show:

concatenatedProperties

Array

Defines the properties that will be concatenated from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by combining the superclass' property value with the subclass' value. An example of this in use within Ember is the classNames property of Ember.View.

Here is some sample code showing the difference between a concatenated property and a normal one:

1
2
3
4
5
6
7
8
9
10
11
12
13
App.BarView = Ember.View.extend({
  someNonConcatenatedProperty: ['bar'],
  classNames: ['bar']
});

App.FooBarView = App.BarView.extend({
  someNonConcatenatedProperty: ['foo'],
  classNames: ['foo'],
});

var fooBarView = App.FooBarView.create();
fooBarView.get('someNonConcatenatedProperty'); // ['foo']
fooBarView.get('classNames'); // ['ember-view', 'bar', 'foo']

This behavior extends to object creation as well. Continuing the above example:

1
2
3
4
5
6
var view = App.FooBarView.create({
  someNonConcatenatedProperty: ['baz'],
  classNames: ['baz']
})
view.get('someNonConcatenatedProperty'); // ['baz']
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Adding a single property that is not an array will just add it in the array:

1
2
3
4
var view = App.FooBarView.create({
  classNames: 'baz'
})
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Using the concatenatedProperties property, we can tell to Ember that mix the content of the properties.

In Ember.View the classNameBindings and attributeBindings properties are also concatenated, in addition to classNames.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently.

Default: null

currentPath

String

The path of the current state. Returns a string representation of the current state.

currentState

Ember.State

The current state from among the manager's possible states. This property should not be set directly. Use transitionTo to move between states by name.

errorOnUnhandledEvents

Boolean

If set to true, errorOnUnhandledEvents will cause an exception to be raised if you attempt to send an event to a state manager that is not handled by the current state or any of its parent states.

Default: true

hasContext

A boolean value indicating whether the state takes a context. By default we assume all states take contexts.

Default: true

isDestroyed

Destroyed object property flag.

if this property is true the observers and bindings were already removed by the effect of calling the destroy() method.

Default: false

isDestroying

Destruction scheduled flag. The destroy() method has been called.

The object stays intact until the end of the run loop at which point the isDestroyed flag is set.

Default: false

isLeaf

Boolean

A Boolean value indicating whether the state is a leaf state in the state hierarchy. This is false if the state has child states; otherwise it is true.

name

String

The name of this state.

parentState

Ember.State

isState: true,

/** A reference to the parent state.

path

String

The full path to this state.

transitionEvent

String

The name of transitionEvent that this stateManager will dispatch

Default: 'setup'

Show:

enter

(manager)

This event fires when the state is entered.

Parameters:

manager Ember.StateManager

exit

(manager)

This event fires when the state is exited.

Parameters:

manager Ember.StateManager

setup

(manager, context)

This is the default transition event.

Parameters:

manager Ember.StateManager
context