Skip to content

Commit

Permalink
version 2.0: fun() expects Promise<string>
Browse files Browse the repository at this point in the history
and npm upgrade
  • Loading branch information
NicolasCARPi committed Feb 22, 2023
1 parent 4a55b46 commit a9d314c
Show file tree
Hide file tree
Showing 7 changed files with 1,169 additions and 782 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for malle

## 2.0.0

* The `fun` function now expects a `Promise<string>` as return value

## 1.0.1

* Switch eslint-plugin-node (unmaintained) to eslint-plugin-n
Expand Down
6 changes: 2 additions & 4 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ default: `true`
Bring the input into focus.

#### fun (required)
`Function(value: string, original: HTMLElement, event: Event, input: HTMLInputElement): string`
`Function(value: string, original: HTMLElement, event: Event, input: HTMLInputElement): Promise<string>`
default: `undefined`

This is your function doing the hard work. It must return the `value` as a string, and this will be the new value of the target element. Use the original element, the event or the input to achieve what you want. Typically, this function will make a POST or PUT request to update the value in the backend.
This is your function doing the hard work. It must return the `value` as a `Promise<string>`, and this string will be the new value of the target element. Use the original element, the event or the input to achieve what you want. Typically, this function will make a POST or PUT request to update the value in the backend.

#### listenNow
boolean
Expand Down Expand Up @@ -166,5 +166,3 @@ string
default: `''`

Set the title of the elements we listen on. You could use something like `Click to edit!`. The text will be visible if the mouse is hovering the element.


6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ const malle = new Malle({
console.log(`Original element:`);
console.log(original);
// add here your code for POSTing the new value
// something along the line of:
return fetch('/ajax', {
method: 'POST',
body: JSON.stringify({ 'name': value, 'id': original.dataset.id }),
});
return myFunctionReturingAPromiseString();
},
}).listen(); // directly start listening after object creation
~~~
Expand Down
6 changes: 3 additions & 3 deletions demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { Action, InputType, Malle } = await import(libPath);
const myCustomFunction = (value, orig) => {
console.log(`New text: ${value}`);
// do something with that value, like POSTing it somewhere
return value;
return new Promise(resolve => resolve(value));
};

// minimal options
Expand Down Expand Up @@ -66,7 +66,7 @@ const malle = new Malle({
// this has no listenOn option so it will listen on all data-malleable='true' elements
new Malle({
fun: value => {
return value;
return new Promise(resolve => resolve(value));
},
formClasses: ['d-inline-flex'],
onBlur: Action.Ignore,
Expand All @@ -76,7 +76,7 @@ new Malle({

new Malle({
fun: value => {
return value;
return new Promise(resolve => resolve(value));
},
inputType: InputType.Select,
selectOptions: [
Expand Down
Loading

0 comments on commit a9d314c

Please sign in to comment.