best way to execute eternal js

72 views
Skip to first unread message

luis.va...@metamaxzone.com

unread,
Dec 23, 2015, 9:46:41 AM12/23/15
to web2py-users
Hello!

I've been analyzing how to work around in the best form an "issue", this doesnt affect the functionality of my code but it is against good practices.

My website have a full internationalization and uses some variables passed by the controller inside the javascript to generate a highly dynamical page, this is no problem if i have my javascript inline my HTML templates, but for sake of order and good practices i wish to have my .js and .html in different files, but if i've the js outside my template it doesnt know how to process a web2py helper instruction.

So i've been thinking in some workarounds but i dont find a good solution for now, may be some of you have a suggestion for this.

Thank you very much!

Anthony

unread,
Dec 23, 2015, 11:37:46 AM12/23/15
to web2py-users
Are you saying some of your Javascript code is generated via web2py templates? If so, it's hard to say what the best approach would be without seeing some code.

Anthony

luis.va...@metamaxzone.com

unread,
Dec 23, 2015, 12:48:03 PM12/23/15
to web2py-users
Yes, inside my javascript i have a lot of web2py helpers for string internationalization and even for looping between a dictionary of data and generating a set of markers for google maps, also i use the web2py URL helpers for the ajax requests URL. I can change the way i handle the loop and the URL, but for internationalization i have not found any way to handle the internationalization

Anthony

unread,
Dec 23, 2015, 1:01:00 PM12/23/15
to web2py-users
If you're just worried about code organization and want the JS code in a separate file, you could move it to its own template and then include it where needed. For example, move your JS code to a file like /views/my.js. Then in your view:

<script>
{{include 'my.js'}}
</script>

However, that will be somewhat inefficient if you've got lots of Javascript code but only a little bit of dynamically generated web2py content mixed in. An alternative is to define the dynamically generated web2py variables in one block of Javascript in the template, and then load a static JS file that simply refers to those variables (this is how web2py_ajax.html works -- notice that it defines several JS variables, which are then used by web2py.js). So, in your view:

<script>
var someVar = '{{=T('some phrase to be translated')}}';
var someURL = '{{=URL('default', 'myfunction')}}';
</script>
<script src="{{=URL('static', 'js', 'my.js')}}"></script>

Then in my.js, you can refer to someVar and someURL.

Anthony

luis.va...@metamaxzone.com

unread,
Dec 23, 2015, 3:50:58 PM12/23/15
to web2py-users
the first solution is a very good approach to my problem because i've a considerable amount of python code inside my javascript, in fact if i define variables for this content i will have between 80-120 vars per view. But i still have one problem with this solution: For the navigator is exactly like i have the code inline with my html and this mean that it'll not be cached as a javascript file. It would be really good if the navigator could cache this file.

Thanks for your help!

Anthony

unread,
Dec 23, 2015, 4:01:52 PM12/23/15
to web2py-users
On Wednesday, December 23, 2015 at 3:50:58 PM UTC-5, luis.va...@metamaxzone.com wrote:
the first solution is a very good approach to my problem because i've a considerable amount of python code inside my javascript, in fact if i define variables for this content i will have between 80-120 vars per view. But i still have one problem with this solution: For the navigator is exactly like i have the code inline with my html and this mean that it'll not be cached as a javascript file. It would be really good if the navigator could cache this file.

If the content of the JS doesn't depend on other data associated with the page request, you could serve the JS via a separate request and cache it:

@cache.action(...)
def serve_js():
    response
.view = 'views/js/%s' % request.args(0)
   
return dict()

Then in your main view:

<script src="{{=URL('default', 'serve_js', args='my.js')}}"></script>

Anthony

luis.va...@metamaxzone.com

unread,
Dec 23, 2015, 9:31:57 PM12/23/15
to web2py-users
Okay this is a really great idea and i can use it perfectly, but i wonder (only for making it easy to me) if there is a way to do this with a javascript that depends of the data associated with the page request? maybe a way to pass all the params recieved by the view from the controller to my server_js method in order to pass this parameters also to the .js file.

Anthony

unread,
Dec 23, 2015, 10:22:40 PM12/23/15
to web2py-users
On Wednesday, December 23, 2015 at 9:31:57 PM UTC-5, luis.va...@metamaxzone.com wrote:
Okay this is a really great idea and i can use it perfectly, but i wonder (only for making it easy to me) if there is a way to do this with a javascript that depends of the data associated with the page request? maybe a way to pass all the params recieved by the view from the controller to my server_js method in order to pass this parameters also to the .js file.

If the JS depends on data specific to the page, then there is probably not much benefit to caching it separately -- you might as well just cache the whole page. If the page isn't cacheable due to constantly changing data, then by definition neither is the JS, which also depends on that data.

Anyway, passing the data to the serve_js function solely via URL can be tricky -- that will only work for simply data structures such as strings and integers (e.g., you can't pass a Rows object via URL).

Anthony
Reply all
Reply to author
Forward
0 new messages