title |
---|
Observable |
Provides support for firing events. Inherits from kendo.Class.
Attaches a handler to an event.
The name of the event.
A function to execute each time the event is triggered. That function should have a single parameter which will contain any event specific data.
Important: The context (
this
) of thehandler
function is set to the observable object itself.
<script>
var obj = new kendo.Observable();
obj.bind("myevent", function(e) {
console.log(e.sender === obj); // outputs "true"
console.log(this === obj); // also outputs "true"
});
obj.trigger("myevent"); // causes the handler to be executed
</script>
Attaches a handler to an event. The handler is executed only once.
The name of the event.
A function to execute each time the event is triggered. That function should have a single parameter which will contain any event specific data.
Important: The context (
this
) of thehandler
function is set to the observable object itself.
<script>
var obj = new kendo.Observable();
var numberOfCalls = 0;
obj.one("myevent", function() {
numberOfCalls ++; // increment the counter every time the handler is executed
});
obj.trigger("myevent"); // fire the event
obj.trigger("myevent"); // fire the event
console.log(numberOfCalls); // outputs "1"
</script>
Executes all handlers attached to the given event.
The name of the event to trigger.
Optional event data which will be passed as an argument to the event handlers.
<script>
var obj = new kendo.Observable();
obj.bind("myevent", function(e) {
console.log(e.data); // outputs "data"
});
obj.trigger("myevent", { data: "data" });
</script>
Remove a previously attached event handler.
The name of the event. If not specified all handlers of all events will be removed.
The handler which should no longer be executed. If not specified all handlers listening to that event will be removed.
<script>
var obj = new kendo.Observable();
var numberOfCalls = 0;
function handler(e) {
numberOfCalls ++;
}
obj.bind("myevent", handler); // subscribe to the event
obj.trigger("myevent"); // fire the event
obj.unbind("myevent", handler); // unsubscribe from the event
obj.trigger("myevent"); // fire the event
console.log(numberOfCalls); // outputs "1"
</script>