Available events

Models have several different events available for you to listen to, as shown in the following table:

Event name

Trigger

change

When any attribute of a Model changes

change:attribute

When the specified attribute changes

destroy

When the Model is destroyed

request

When an AJAX method of the Model starts

sync

When an AJAX method of the Model has completed

error

When an AJAX method of the Model returns an error

invalid

When validation triggered by a Model's save/isValid call fails

There are also several other events that are related to Collection, which will be explained further in the next chapter:

Event name

Trigger

add

When the Model is added to a Collection

remove

When the Model is removed from a Collection

reset

When a Collection that the Model belongs to is reset

Models also have one other, special, event called all. This event is triggered in response when any of the other Model events are triggered.

Custom events

Although you are unlikely to use them very often, you may find it useful to create your own custom events on certain occasions. This can be done by using the Model's trigger method, which lets you simulate an event coming from the Model, as follows:

someModel.trigger('fakeEvent', 5);

You can listen for these events in the way same as any other non-custom event, by using the on method. Any additional arguments passed to trigger (such as the 5 in the preceding example) will be passed as arguments to event handlers that listen for that event.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.105.105