Skip to content

Add ol.Observable.unByKey #2794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2014
Merged

Add ol.Observable.unByKey #2794

merged 1 commit into from
Oct 8, 2014

Conversation

elemoine
Copy link
Member

@elemoine elemoine commented Oct 3, 2014

Today we have ol.Observable.prototype.unByKey:

/**
* Removes an event listener using the key returned by `on()` or `once()`.
* @param {goog.events.Key} key Key.
* @api stable
*/
ol.Observable.prototype.unByKey = function(key) {
  goog.events.unlistenByKey(key);
};

So it is a method wrapping a static function. That means it could instead be static:

/**
* Removes an event listener using the key returned by `on()` or `once()`.
* @param {goog.events.Key} key Key.
* @api stable
*/
ol.Observable.unByKey = function(key) {
  goog.events.unlistenByKey(key);
};

Having a static function (ol.Observable.unByKey) would be more convenient. Because when it is the time to unregister your listeners you may not have a reference to the object you registered your listeners on. You may even have an array of listener ids for listeners on multiple observables.

Please review.

We could also consider @deprecate'ing ol.Observable.prototype.unByKey.

@elemoine elemoine added the api label Oct 3, 2014
@elemoine elemoine added this to the v3.1.0 milestone Oct 3, 2014
@fredj
Copy link
Member

fredj commented Oct 3, 2014

LGTM and +1 on deprecating ol.Observable.prototype.unByKey

@@ -99,5 +109,5 @@ ol.Observable.prototype.un = function(type, listener, opt_this) {
* @api stable
*/
ol.Observable.prototype.unByKey = function(key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function could be a simple alias:

ol.Observable.prototype.unByKey = ol.Observable.unByKey;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the API docs does not look good if I use:

ol.Observable.prototype.unByKey = ol.Observable.unByKey;

This is what it looks like:

with-alias

The unByKey method is placed in a Members section, and it is not with the other methods.

So I decided not to use the alias.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A @function jsdoc tag is probably needed in this case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indeed works! I keep forgetting about that annotation. I'm amending the commit. Thanks.

@tschaub
Copy link
Member

tschaub commented Oct 5, 2014

Looks good to me too. Hard to imagine 4.0 at this point, and it feels sloppy to have something deprecated immediately after 3.0. So I wouldn't be motivated to add anything to the docs (it would show up on almost every type), but that's a personal opinion.

@elemoine
Copy link
Member Author

elemoine commented Oct 5, 2014

Agree with @tschaub. I'll add in the docs that using the static function rather than the method is encouraged.

@fredj
Copy link
Member

fredj commented Oct 8, 2014

Thanks, please merge

elemoine pushed a commit that referenced this pull request Oct 8, 2014
Add ol.Observable.unByKey
@elemoine elemoine merged commit d1973a0 into openlayers:master Oct 8, 2014
@elemoine elemoine deleted the unbykey branch October 8, 2014 13:14
gberaudo added a commit to gberaudo/openlayers that referenced this pull request Nov 19, 2014
The ol.Observable.prototype.unByKey was supposed to handle removing of
listeners registered using `ol.Observable.prototype.on` and
`ol.Observable.prototype.once`.

However, the underlying google library code called by unByKey only requires
the key, not the `ol.Observable` object on which the registration was
triggered. Moreover, this method was unconvenient and in practice misused.
Example or real world code:
```javascript
var map = new ol.Map();
var key = map.on(listener);
view.unByKey(key);
```

A function was added in openlayers#2794, in particular to fix this kind of code.

This PR pushes forward the removal process of the old method in favor of the
unByKey function by marking it `@deprecated` and pointing to the right
usage.

Please merge when deemed appropriate.
gberaudo added a commit to gberaudo/openlayers that referenced this pull request Nov 19, 2014
The ol.Observable.prototype.unByKey was supposed to handle removing of
listeners registered using `ol.Observable.prototype.on` and
`ol.Observable.prototype.once`.

However, the underlying google library code called by unByKey only requires
the key, not the `ol.Observable` object on which the registration was
triggered. Moreover, this method was unconvenient and in practice misused.
Example or real world code:
```javascript
var map = new ol.Map();
var key = map.on(listener);
view.unByKey(key);
```

A function was added in openlayers#2794, in particular to fix this kind of code.

This PR pushes forward the removal process of the old method in favor of the
unByKey function by marking it `@deprecated` and pointing to the right
usage.

Please merge when deemed appropriate.
gberaudo added a commit to gberaudo/openlayers that referenced this pull request Nov 19, 2014
The ol.Observable.prototype.unByKey was supposed to handle removing of
listeners registered using `ol.Observable.prototype.on` and
`ol.Observable.prototype.once`.

However, the underlying google library code called by unByKey only requires
the key, not the `ol.Observable` object on which the registration was
triggered. Moreover, this method was unconvenient and in practice misused.
Example or real world code:
```javascript
var map = new ol.Map();
var key = map.on(listener);
view.unByKey(key);
```

A function was added in openlayers#2794, in particular to fix this kind of code.

This PR pushes forward the removal process of the old method in favor of the
unByKey function by marking it `@deprecated` and pointing to the right
usage.

Please merge when deemed appropriate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants