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

How to make livescript scripts immediately .go() on seen? #731

Closed
ceremcem opened this issue May 19, 2015 · 4 comments
Closed

How to make livescript scripts immediately .go() on seen? #731

ceremcem opened this issue May 19, 2015 · 4 comments

Comments

@ceremcem
Copy link

When I use LiveScript in an html file directly, I have no way to run livescript code immediately on seen. For example;

...
<script src="livescript.js"></script>

<div class="my_target"></div>

<script type="text/ls">
# my livescript code will interact with div.my_target
</script>

<script>
/* a javascript code that will interact with div.my_target
</script>

<script type="text/ls">
# my livescript code does something else
</script>

<script>
    var LiveScript = require("LiveScript");
    LiveScript.go();
</script>

The LiveScript codes both will run, but the LiveScript code that interacts with div.my_target will interact with it AFTER the javascript code does, not BEFORE

If I define

<script>
    var LiveScript = require("LiveScript");
    LiveScript.go();
</script>

part everytime right after a LiveScript code defined, then all LiveScript codes till this definition would run more than one time.

### livescript code 1
### LiveScript.go()
...
### livescript code 2
### LiveScript.go()
...
### livescript code 3
### LiveScript.go()
...

When this code is executed:

  • livescript code 1 will run 3 times, first will run immediately after definition
  • livescript code 2 will run 2 times, first will run immediately after definition
  • livescript code 3 will run 1 time, this will runimmediately after definition

If it would be this way, LiveScript would be used in html more easily and would be used like a native language of web development

...
<script src="livescript.js"></script>
<script>
    var LiveScript = require("LiveScript");
    LiveScript.doSomeMagic_Kaboooommm();
</script>

...
<script type="text/ls">
# my livescript code
</script>

... more html

<script type="text/ls">
# my livescript code
</script>

...

<script type="text/ls">
# my livescript code
</script>
...

Is there anyway to do that?

@ceremcem
Copy link
Author

The only way I could find is:

<script type="text/ls" id="popup">
    console.log "hello there"
</script>
<script type="text/javascript">
    require("LiveScript").stab($("script#popup").html(), null, null);
</script>

@chisui
Copy link

chisui commented May 20, 2015

Maybe you should consider moving your inline scripts to files and precompiling them.

I don't think it would be possible at all to intercept the loading of script tags. the magic_go() function would have to know what scripts come after it before they are even in the dom or stop javascript from executing when it hits the dom.

@dead-claudia
Copy link
Contributor

If your browser supports MutationObservers, you can include the mutation-summary library and add this (untested) code before any LiveScript scripts:

# Obviously this needs transpiled

# Get the dependency (we need it synchronously)
elem = document.createElement 'script'
elem.src = '//raw.githubusercontent.com/rafaelw/mutation-summary/master/src/mutation-summary.js'
elem.async = false
document.currentScript.parentNode.insertBefore elem, document.currentScript

# Create the observer
new MutationSummary do
  queries: [element: 'script[type="text/ls"]']
  callback: (.0.added.forEach !-> LiveScript.run it.text)

Don't add LiveScript.go() to your page, or your scripts will run twice.

And FWIW, @chisui has a point in that it is probably better to precompile them.

(Edit: fix to not add to head, as it may not yet be loaded)

@ceremcem
Copy link
Author

I'm going to close this because I accepted the precompilation method is the appropriate way to go. Thanks.

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

3 participants