problem with $.proxy scope
I'm trying to create a very simple object that represents an element; in that object, I bind to the 'click' method of some links so the element can handle that action on its own, without the roundabout way of going through global objects, singletons, or more jQuery contexts. I use
$.proxy() to maintain the context of "this" object from the element, but it errors out with "not a function". Perhaps I'm making a simple mistake with scoping; I'm not sure. Can anyone help?
Here's the (simplified) code:
- function article_prototype($article) {
- this._activate = function() {
- this.$.show();
- };
- this._deactivate = function() {
- this.$.hide();
- };
- this.init = function() {
- // Deactivate when other articles' nav links are clicked.
- $('.nav-link:not(#' + this.id + '-link')')
- .bind('click', $.proxy(this, function() {
- this._deactivate();
- }));
- // Activate when this articles' nav link is clicked.
- $('.nav-link#' + this.id + '-link')
- .bind('click', $.proxy(this, function() {
- this._activate();
- }));
- };
- };
- function article($article) {
- this.$ = $article;
- this.id = $article.attr('id');
- this.init();
- };
- article.prototype = new article_prototype;
- $(function() {
- $('.article').each(function() {
- new article($(this));
- });
- });
The error message is: this._deactivate is not a function
I tried using a reference to "this" as a local variable "t" and passing "t" as the first argument of $.proxy, but I still got the same error message.
I'm fairly tired, so I admit I may be overlooking something obvious.