Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ecmel/refable

Super simple JS framework.
https://github.com/ecmel/refable

framework npm-package stimulus

Last synced: 4 months ago
JSON representation

Super simple JS framework.

Awesome Lists containing this project

README

        

# Refable

Super simple JS framework inspired by [Stimulus](https://github.com/hotwired/stimulus).

## Installation

```bash
npm install refable --save-dev
```

## Application

Application is the main class for bootstrapping. Controllers are registered on an application instance. For registering glob of controllers please refer to your bundler's documentation.

```ts
import { Application } from "refable";
import Search from "./controllers/search";

const application = new Application();

application.register("search", Search);

application.run();
```

## Controllers

Controllers are instances of classes that you register in your application. Each controller class inherits from the Controller base class. Controllers can be nested within controllers and can be referenced in the parent controller.

```html




```

```ts
import { Controller } from "refable";
import Result from "./controllers/result";

export default class extends Controller {
declare readonly resultController: Result;
declare readonly resultControllers: Result[];

created() {
//
}

connected() {
//
}

disconnected() {
//
}

resultControllerConnected(result: Result) {
//
}

resultControllerDisconnected(result: Result) {
//
}
}
```

Controller classes are templated so more specific elements can be used if needed.

```ts
import { Controller } from "refable";

export default class extends Controller {
//
}
```

## Values

```html


```

```ts
import { Controller } from "refable";

export default class extends Controller {
declare readonly someValue: string;

someValueChanged(value: string) {
//
}
}
```

## Targets

Targets map important elements to controller properties.

```html




```

```ts
import { Controller } from "refable";

export default class extends Controller {
declare readonly resultTarget: Element;
declare readonly resultTargets: Element[];

resultTargetConnected(el: Element) {
//
}

resultTargetDisconnected(el: Element) {
//
}
}
```

## Actions

Actions are for handling DOM events in controllers.

```html


Find

```

```ts
import { Controller } from "refable";

export default class extends Controller {
find() {
//
}
}
```

### Action Options

You can append one or more action options to an action descriptor if you need to specify event listener options.

```html
Find
```

Following action options are supported:

```
:capture
:once
:passive
:stop
:prevent
```

## Events

The Controller class has a method called dispatch that fires custom events. It takes an event name and an optional payload as arguments.

```html


Find

```

```ts
import { Controller } from "refable";

export default class extends Controller {
find(event: Event) {
this.dispatch("found", { result: "found" });
}
}
```

The dispatched event can be catched with an action in a parent element and handled in a different controller like an action. Optional payload is in event parameter's detail property.

## Plain JS Example

```html






import {
Application,
Controller,
} from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";

class Home extends Controller {
connected() {
console.log("Connected");
}
}

const application = new Application();

application.register("home", Home);
application.run();

```