Sneaky Abstractions

Subscribe to my Feed, follow me on , recommend me on Working With Rails or see my code on GitHub

Comment

You're viewing a comment to the post Unobtrusive Ajax patterns: element state

By tobielangel.com at

Of course, you could also avoid the constructor altogether and use a closure instead, favoring encapsulation over memory consumption.

Mysite = {};

Mysite.posts = $$('.post').map(function(element) {
  return {
    markRead: function(){
      element.addClassName('read');
    },
    markUnread: function(){
      element.removeClassName('read');
    },
    isRead: function(){
      return element.hasClassName('read');
    }
  };
});

They tried the freeway, they tried the highway, they tried any which way, and they were taken down!