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

feature: add save hook to atoms #399

Closed
bantic opened this issue May 12, 2016 · 1 comment
Closed

feature: add save hook to atoms #399

bantic opened this issue May 12, 2016 · 1 comment

Comments

@bantic
Copy link
Collaborator

bantic commented May 12, 2016

As requested in gitter by @t4t5, atoms should have a save hook to allow programmatically updating their value and/or payload.

bantic added a commit that referenced this issue Aug 25, 2016
Save hook is on the atom's `env` and accepts arguments: `(value, payload)`.
Calling the save hook rerenders the atom.

Example:
```
let atom = {
  name: 'my-atom',
  type: 'dom',
  render({env, value, payload}) {
    let el = document.createElement('button');
    let clicks = payload.clicks || 0;
    el.appendChild(document.createTextNode('Clicks: ' + clicks));
    el.onclick = () => {
      payload.clicks = payload.clicks || 0;
      payload.clicks++;
      env.save(value, payload);
    };
    return el;
  }
};
```

Also: improve postAbstract buildFromText to accept data for an atom,
i.e. "abc@(...jsondata...)", e.g.:
```
buildFromText('abc@("name": "my-atom", "value": "bob", "payload": {"foo": "bar"})def');
// -> "abc" + atom with name "my-atom", value "bob", payload {foo: 'bar'} + "def"
```

Fixes #399
bantic added a commit that referenced this issue Aug 25, 2016
Save hook is on the atom's `env` and accepts arguments: `(value, payload)`.
Calling the save hook rerenders the atom.

Example:
```
let atom = {
  name: 'my-atom',
  type: 'dom',
  render({env, value, payload}) {
    let el = document.createElement('button');
    let clicks = payload.clicks || 0;
    el.appendChild(document.createTextNode('Clicks: ' + clicks));
    el.onclick = () => {
      payload.clicks = payload.clicks || 0;
      payload.clicks++;
      env.save(value, payload);
    };
    return el;
  }
};
```

Also: improve postAbstract buildFromText to accept data for an atom,
i.e. "abc@(...jsondata...)", e.g.:
```
buildFromText('abc@("name": "my-atom", "value": "bob", "payload": {"foo": "bar"})def');
// -> "abc" + atom with name "my-atom", value "bob", payload {foo: 'bar'} + "def"
```

Fixes #399
bantic added a commit that referenced this issue Aug 25, 2016
Save hook is on the atom's `env` and accepts arguments: `(value, payload)`.
Calling the save hook rerenders the atom.

Example:
```
let atom = {
  name: 'my-atom',
  type: 'dom',
  render({env, value, payload}) {
    let el = document.createElement('button');
    let clicks = payload.clicks || 0;
    el.appendChild(document.createTextNode('Clicks: ' + clicks));
    el.onclick = () => {
      payload.clicks = payload.clicks || 0;
      payload.clicks++;
      env.save(value, payload);
    };
    return el;
  }
};
```

Also: improve postAbstract buildFromText to accept data for an atom,
i.e. "abc@(...jsondata...)", e.g.:
```
buildFromText('abc@("name": "my-atom", "value": "bob", "payload": {"foo": "bar"})def');
// -> "abc" + atom with name "my-atom", value "bob", payload {foo: 'bar'} + "def"
```

Fixes #399
@bantic
Copy link
Collaborator Author

bantic commented Aug 25, 2016

released in v0.10.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant