Skip to content
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

Components as extended classes #8

Open
adamjgrant opened this issue Jun 17, 2021 · 1 comment
Open

Components as extended classes #8

adamjgrant opened this issue Jun 17, 2021 · 1 comment
Labels

Comments

@adamjgrant
Copy link
Owner

adamjgrant commented Jun 17, 2021

Basic setup of a component follows native JS pattern.

class Card extends Component {
  constructor() {
    this.classname = "card";
  }

  events(_) {
    // Just a wrapper for addEventListener scoped only for class="approve" elements in a card component.
    // And "_" gives you access to the component's API.
    _.e("click", ".approve", _.approve);
    _.e("click", ".reject", _.reject);
  }

  api() {
    // This is a separate component we made elsewhere.
    const notification = new Notification();

    return {
      approve(_) { _.set_color("green"); },
      reject(_)  { _.set_color("red"); },

      set_color(color, _) {
        _.me.classList.add(`card-${color}`);
        _.save_card_to_database({
          id: _.me.index,
          color: color
        });

        // Call the api of another component
        notification.show("Color has been changed");
      },

      save_card_to_database(data, _) {
        // ...
      }      
    }
  }
}

To get an array of the cards with all the functional goodies:

const cards = Card.elements; // NodeList[]

Another idea

cards.e("click", "button").then(_ => {
  _.set_color("green");
});

These events will only fire for elements with the specified classname and their children.

<div class="card">
  <button>Press me</button>
</div>

<div class="something-else">
  <button>I don't do anything</button>
</div>
@adamjgrant
Copy link
Owner Author

Still a rough idea but trying to get my thoughts down....

@adamjgrant adamjgrant added the v3 label Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant