Return emitter in #on(), #off(), #once(), so calls can be chained.

This commit is contained in:
Jürg Lehni 2014-11-18 14:56:55 -08:00
parent 0fb7e49248
commit 19a9976939
4 changed files with 26 additions and 7 deletions

View file

@ -36,6 +36,7 @@ var Emitter = {
entry.install.call(this, type); entry.install.call(this, type);
} }
} }
return this;
}, },
off: function(type, func) { off: function(type, func) {
@ -62,10 +63,11 @@ var Emitter = {
handlers.splice(index, 1); handlers.splice(index, 1);
} }
} }
return this;
}, },
once: function(type, func) { once: function(type, func) {
this.on(type, function() { return this.on(type, function() {
func.apply(this, arguments); func.apply(this, arguments);
this.off(type, func); this.off(type, func);
}); });

View file

@ -3555,6 +3555,7 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* type * type
* @param {Function} function The function to be called when the event * @param {Function} function The function to be called when the event
* occurs * occurs
* @return {Item} this item itself, so calls can be chained
* *
* @example {@paperscript} * @example {@paperscript}
* // Change the fill color of the path to red when the mouse enters its * // Change the fill color of the path to red when the mouse enters its
@ -3584,7 +3585,8 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* @function * @function
* @param {Object} object an object literal containing one or more of the * @param {Object} object an object literal containing one or more of the
* following properties: {@code mousedown, mouseup, mousedrag, click, * following properties: {@code mousedown, mouseup, mousedrag, click,
* doubleclick, mousemove, mouseenter, mouseleave}. * doubleclick, mousemove, mouseenter, mouseleave}
* @return {Item} this item itself, so calls can be chained
* *
* @example {@paperscript} * @example {@paperscript}
* // Change the fill color of the path to red when the mouse enters its * // Change the fill color of the path to red when the mouse enters its
@ -3643,6 +3645,7 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* 'doubleclick', 'mousemove', 'mouseenter', 'mouseleave')} type the event * 'doubleclick', 'mousemove', 'mouseenter', 'mouseleave')} type the event
* type * type
* @param {Function} function The function to be detached * @param {Function} function The function to be detached
* @return {Item} this item itself, so calls can be chained
*/ */
/** /**
* Detach one or more event handlers to the item. * Detach one or more event handlers to the item.
@ -3652,6 +3655,7 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* @param {Object} object an object literal containing one or more of the * @param {Object} object an object literal containing one or more of the
* following properties: {@code mousedown, mouseup, mousedrag, click, * following properties: {@code mousedown, mouseup, mousedrag, click,
* doubleclick, mousemove, mouseenter, mouseleave} * doubleclick, mousemove, mouseenter, mouseleave}
* @return {Item} this item itself, so calls can be chained
*/ */
/** /**
@ -3663,7 +3667,8 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* 'doubleclick', 'mousemove', 'mouseenter', 'mouseleave')} type the event * 'doubleclick', 'mousemove', 'mouseenter', 'mouseleave')} type the event
* type * type
* @param {Object} event an object literal containing properties describing * @param {Object} event an object literal containing properties describing
* the event. * the event
* @return {Boolean} {@true if the event had listeners}
*/ */
/** /**

View file

@ -417,6 +417,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* 'keydown', 'keyup')} type the event type * 'keydown', 'keyup')} type the event type
* @param {Function} function The function to be called when the event * @param {Function} function The function to be called when the event
* occurs * occurs
* @return {Tool} this tool itself, so calls can be chained
*/ */
/** /**
* Attach one or more event handlers to the tool. * Attach one or more event handlers to the tool.
@ -425,7 +426,8 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* @function * @function
* @param {Object} param an object literal containing one or more of the * @param {Object} param an object literal containing one or more of the
* following properties: {@code mousedown, mouseup, mousedrag, mousemove, * following properties: {@code mousedown, mouseup, mousedrag, mousemove,
* keydown, keyup}. * keydown, keyup}
* @return {Tool} this tool itself, so calls can be chained
*/ */
/** /**
@ -436,6 +438,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* @param {String('mousedown', 'mouseup', 'mousedrag', 'mousemove', * @param {String('mousedown', 'mouseup', 'mousedrag', 'mousemove',
* 'keydown', 'keyup')} type the event type * 'keydown', 'keyup')} type the event type
* @param {Function} function The function to be detached * @param {Function} function The function to be detached
* @return {Tool} this tool itself, so calls can be chained
*/ */
/** /**
* Detach one or more event handlers from the tool. * Detach one or more event handlers from the tool.
@ -445,6 +448,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* @param {Object} param an object literal containing one or more of the * @param {Object} param an object literal containing one or more of the
* following properties: {@code mousedown, mouseup, mousedrag, mousemove, * following properties: {@code mousedown, mouseup, mousedrag, mousemove,
* keydown, keyup} * keydown, keyup}
* @return {Tool} this tool itself, so calls can be chained
*/ */
/** /**
@ -455,7 +459,8 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* @param {String('mousedown', 'mouseup', 'mousedrag', 'mousemove', * @param {String('mousedown', 'mouseup', 'mousedrag', 'mousemove',
* 'keydown', 'keyup')} type the event type * 'keydown', 'keyup')} type the event type
* @param {Object} event an object literal containing properties describing * @param {Object} event an object literal containing properties describing
* the event. * the event
* @return {Boolean} {@true if the event had listeners}
*/ */
/** /**

View file

@ -489,7 +489,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
// DOCS: projectToView(point), viewToProject(point) // DOCS: projectToView(point), viewToProject(point)
/** /**
* @param {Point} * @param {Point} point
* @return {Point} * @return {Point}
*/ */
projectToView: function(/* point */) { projectToView: function(/* point */) {
@ -497,7 +497,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
}, },
/** /**
* @param {Point} * @param {Point} point
* @return {Point} * @return {Point}
*/ */
viewToProject: function(/* point */) { viewToProject: function(/* point */) {
@ -564,6 +564,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @param {String('frame', 'resize')} type the event type * @param {String('frame', 'resize')} type the event type
* @param {Function} function The function to be called when the event * @param {Function} function The function to be called when the event
* occurs * occurs
* @return {View} this view itself, so calls can be chained
* *
* @example {@paperscript} * @example {@paperscript}
* // Create a rectangle shaped path with its top left point at: * // Create a rectangle shaped path with its top left point at:
@ -585,6 +586,9 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @function * @function
* @param {Object} param an object literal containing one or more of the * @param {Object} param an object literal containing one or more of the
* following properties: {@code frame, resize}. * following properties: {@code frame, resize}.
* @return {View} this view itself, so calls can be chained
*
* @example {@paperscript}
* // Create a rectangle shaped path with its top left point at: * // Create a rectangle shaped path with its top left point at:
* // {x: 50, y: 25} and a size of {width: 50, height: 50} * // {x: 50, y: 25} and a size of {width: 50, height: 50}
* var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50)); * var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50));
@ -607,6 +611,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @function * @function
* @param {String('frame', 'resize')} type the event type * @param {String('frame', 'resize')} type the event type
* @param {Function} function The function to be detached * @param {Function} function The function to be detached
* @return {View} this view itself, so calls can be chained
* *
* @example {@paperscript} * @example {@paperscript}
* // Create a rectangle shaped path with its top left point at: * // Create a rectangle shaped path with its top left point at:
@ -636,6 +641,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @function * @function
* @param {Object} param an object literal containing one or more of the * @param {Object} param an object literal containing one or more of the
* following properties: {@code frame, resize} * following properties: {@code frame, resize}
* @return {View} this view itself, so calls can be chained
*/ */
/** /**
@ -646,6 +652,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* @param {String('frame', 'resize')} type the event type * @param {String('frame', 'resize')} type the event type
* @param {Object} event an object literal containing properties describing * @param {Object} event an object literal containing properties describing
* the event. * the event.
* @return {Boolean} {@true if the event had listeners}
*/ */
/** /**