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

Update Documentation for context, initialize, onconnect, & onidle #188

Closed
snuggs opened this issue Feb 12, 2019 · 0 comments
Closed

Update Documentation for context, initialize, onconnect, & onidle #188

snuggs opened this issue Feb 12, 2019 · 0 comments
Assignees

Comments

@snuggs
Copy link
Member

snuggs commented Feb 12, 2019

@0xhjohnson has bought to our attention the need for documentation of intermediate API features.
We actually have documentation @ http://snuggsi.herokuapp.com/#lifecycle
However a little vague to access as domain name is not pointed to heroku instance. Also as @brandondees the "Site is janky" and perhaps need to start thinking of documentation outside of Github.

AN async/await example (like below) could be some nice eye candy as well. Best part of the snuggsi element is it's just a class which in itself is syntactic sugar for a bunch of methods. Therefore technically every member method of a snuggsi element can be async/await. SUPER HANDY when needing to say fetch data from an API in initialize or onconnect. Don't forget about those async button actions that are usually a pain in the butt.

Dependencies

References

Example

Refactor

<hello-world>Coin of the day: {coin}</hello-world>

<script src=//unpkg.com/snuggsi></script>
<script src=https://unpkg.com/axios/dist/axios.min.js></script>
<script>
// Element Definition -----------------------------

Element `hello-world`

// Class Description ------------------------------

(class extends HTMLElement {

  initialize ()  // context
    { this.context.coin = 'Bit'  }

  onidle (e)
    { console.log ('Happens after each render') }

  async onconnect (e) {  // event handler for when custom element is connected to DOM
  
    // Just like jQuery...or any other DOM event handler for that matter
    // One side effect is this prevents rendering which occurs after ALL event handlers.
    e.preventDefault () // or return false

    let // Deconstructing Assignment
        // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
      { response : { data : { chartName : coin } } }
        = await axios.get ('//api.coindesk.com/v1/bpi/currentprice.json')

    this.context.coin = coin
    console.log (this.context.coin)
  }

  get coin () // element properties are bound to tokens. * NOT CONTEXT*
    { return this.context.coin }
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants