max-mapper / yo-yo

A tiny library for building modular UI components using DOM diffing and ES6 tagged template literals
1.33k stars 65 forks source link

Example with AJAX request #62

Open hdriqi opened 7 years ago

hdriqi commented 7 years ago

Can someone provide best practice for ajax request and yo yo template?

juliangruber commented 7 years ago

ajax and templates are two different problem domains and yo yo doesn't aim to solve them both - unlike say jQuery.

There's a bunch of ways you can do Ajax, like the xhr library on npm, or the new fetch api.

If your question is more about the asynchronous nature of ajax requests, can you elaborate more?

hdriqi commented 7 years ago

@juliangruber yes, it is more on asynchronous. For example I want to do ajax request and after that update using yo.update

What is the best way to do that, is it by doing ajax request at the root node and after the data has been received use yo.update(treeID, newTree) ??

juliangruber commented 7 years ago

can you maybe post a code example of what you're doing? not quite sure i follow your example

hdriqi commented 7 years ago
function ajaxReq(){
    doAjaxRequest
    .done((newData) => {
        var newMain = main(newData)
        yo.update(main(), newMain)
    })
}

function main (data) {
  return yo`
    <div>
      ${data}
    </div>
  `
}

yo.update(document.body.firstChild, main())
ajaxReq()

Is this a good practice for doing ajax request?