UNPKG

4.78 kBTypeScriptView Raw
1declare module '@ember/object/evented' {
2 import Mixin from '@ember/object/mixin';
3 export { on } from '@ember/-internals/metal';
4 /**
5 @module @ember/object/evented
6 */
7 /**
8 This mixin allows for Ember objects to subscribe to and emit events.
9
10 ```app/utils/person.js
11 import EmberObject from '@ember/object';
12 import Evented from '@ember/object/evented';
13
14 export default EmberObject.extend(Evented, {
15 greet() {
16 // ...
17 this.trigger('greet');
18 }
19 });
20 ```
21
22 ```javascript
23 var person = Person.create();
24
25 person.on('greet', function() {
26 console.log('Our person has greeted');
27 });
28
29 person.greet();
30
31 // outputs: 'Our person has greeted'
32 ```
33
34 You can also chain multiple event subscriptions:
35
36 ```javascript
37 person.on('greet', function() {
38 console.log('Our person has greeted');
39 }).one('greet', function() {
40 console.log('Offer one-time special');
41 }).off('event', this, forgetThis);
42 ```
43
44 @class Evented
45 @public
46 */
47 interface Evented {
48 /**
49 Subscribes to a named event with given function.
50
51 ```javascript
52 person.on('didLoad', function() {
53 // fired once the person has loaded
54 });
55 ```
56
57 An optional target can be passed in as the 2nd argument that will
58 be set as the "this" for the callback. This is a good way to give your
59 function access to the object triggering the event. When the target
60 parameter is used the callback method becomes the third argument.
61
62 @method on
63 @param {String} name The name of the event
64 @param {Object} [target] The "this" binding for the callback
65 @param {Function|String} method A function or the name of a function to be called on `target`
66 @return this
67 @public
68 */
69 on<Target>(
70 name: string,
71 target: Target,
72 method: string | ((this: Target, ...args: any[]) => void)
73 ): this;
74 on(name: string, method: ((...args: any[]) => void) | string): this;
75 /**
76 Subscribes a function to a named event and then cancels the subscription
77 after the first time the event is triggered. It is good to use ``one`` when
78 you only care about the first time an event has taken place.
79
80 This function takes an optional 2nd argument that will become the "this"
81 value for the callback. When the target parameter is used the callback method
82 becomes the third argument.
83
84 @method one
85 @param {String} name The name of the event
86 @param {Object} [target] The "this" binding for the callback
87 @param {Function|String} method A function or the name of a function to be called on `target`
88 @return this
89 @public
90 */
91 one<Target>(
92 name: string,
93 target: Target,
94 method: string | ((this: Target, ...args: any[]) => void)
95 ): this;
96 one(name: string, method: string | ((...args: any[]) => void)): this;
97 /**
98 Triggers a named event for the object. Any additional arguments
99 will be passed as parameters to the functions that are subscribed to the
100 event.
101
102 ```javascript
103 person.on('didEat', function(food) {
104 console.log('person ate some ' + food);
105 });
106
107 person.trigger('didEat', 'broccoli');
108
109 // outputs: person ate some broccoli
110 ```
111
112 @method trigger
113 @param {String} name The name of the event
114 @param {Object...} args Optional arguments to pass on
115 @public
116 */
117 trigger(name: string, ...args: any[]): any;
118 /**
119 Cancels subscription for given name, target, and method.
120
121 @method off
122 @param {String} name The name of the event
123 @param {Object} target The target of the subscription
124 @param {Function|String} method The function or the name of a function of the subscription
125 @return this
126 @public
127 */
128 off<Target>(
129 name: string,
130 target: Target,
131 method: string | ((this: Target, ...args: any[]) => void)
132 ): this;
133 off(name: string, method: string | ((...args: any[]) => void)): this;
134 /**
135 Checks to see if object has any subscriptions for named event.
136
137 @method has
138 @param {String} name The name of the event
139 @return {Boolean} does the object have a subscription for event
140 @public
141 */
142 has(name: string): boolean;
143 }
144 const Evented: Mixin;
145 export default Evented;
146}
147
\No newline at end of file