ufront / ufront-mvc

The core MVC framework that powers ufront
MIT License
17 stars 15 forks source link

ClientJS:Execute <script> after refreshing the DOM #24

Closed kevinresol closed 8 years ago

kevinresol commented 9 years ago

23

I only tested it on Chrome and scripts was executed nicely.

kevinresol commented 9 years ago

I think there are some glitches for this method. I got some functionalities not (or occasionally) working when the pages are refreshed by the js client. Particularly, I am using UIkit. After refreshing, the nav bar dropdown just won't show (but it sometimes shows again when the page refreshed again).

Need some ways to thoroughly test this. Any ideas?

MichaPau commented 9 years ago

I've got the same glitches with this method. As a (hacky) workaround I gave the script tag an id attribute in CallJavascriptResult.hx:

public function addInlineJs( js:String ):CallJavascriptResult<T> {
        scripts.push( '<script id="ufrontScript" type="text/javascript">$js</script>' );
        return this;
}

public function addJsScript( path:String ):CallJavascriptResult<T> {
    scripts.push( '<script id="ufrontScript" type="text/javascript" src="$path"></script>' );
    return this;
}

and check this attribute in HttpResponse (js):

document.find("script").each(function(node) {
    if (node.attr("id") == "ufrontScript") {
    ...

using it in the controller

var result =  new ViewResult(args).addJsScriptToResult("path/to/script");
return result;
kevinresol commented 9 years ago

Updated. Only script tags with uf-reload attribute will re-run

Examples:

<script uf-reload>console.log("test");</script>
<script uf-reload src="app.js"></script>
kevinresol commented 9 years ago

Next: Need to add one more parameter (reload:Bool) to CallJSResult

public function addInlineJs( js:String, reload:Bool ):CallJavascriptResult<T>;
public function addJsScript( path:String, reload:Bool ):CallJavascriptResult<T>;
MichaPau commented 9 years ago

+1 looks like a nice solution