I use asp.net mvc & going to build javascript files at application startup depending on site configuration.
I am going to have some javascript file templates that will be populated with appropriate constants and put into /scripts folder.
Please, suggest me the best way to do that. I want to have something like:
application_startup()
{
string populatedFile = Html.RenderPartial("/scripts/script.template.js");
write populatedFile into /scripts folder...
}
Thank you in advance !
I'd look at putting the logic in ASHX handler rather than Global.asax, then reference the ASHX as a script reference in your Site Master.
With regards to the actual logic. I'd be pulling your templates using a reader and creating the final script with a String Builder.
If the configuration is not going to change that much, make sure you are caching your output.
Related
I'm using Ruby on Rails framework and I love it.
Until now I was using the show.html.haml for my views and everything worked fine. We wanted to add another option of supporting a GET request which consists the post-fix .js. For this purpose we added to the controller another parallel view named show.js.haml.
The "magic" of Rails knows to route .html post-fix requests to the first and .js post-fix requests to the latter.
The problem is when a request gets routed to the show.js.haml view, I can't find a way to write the content of external .js files. All I get is the text itself (i.e. <script src="myscripts.js" type="text/javascript"></script>) but not the actual content of the file.
I've found a solution, which surely isn't optimal (performance, robustness etc): downloading the file to the server and use the following function:
!= File.open(Rails.root.join('public' , 'js', 'jquery-1.11.2.min.js'), 'rb').read
This is the only way I found to actually write the content of a .js file.
There must be a Ruby's elegant way to write the content of external .js files into a .js.haml file.
Any help would be appreciated here.
Thanks!
I'm not sure why you need this and which case this is a good idea for, but you can try this (untested):
# show.js.haml
// some javascript
= render file: Rails.root.join('public' , 'js', 'jquery-1.11.2.min.js')
// or
"#{render file: Rails.root.join('public' , 'js', 'jquery-1.11.2.min.js')}"
Of course this will only work for local resources accessible, but hey, that's what <script src= is for.
EDIT
In case of needing to store the contents of the file in a variable as a string, you can use capture. For remote files you can use open-uri, which is part of Ruby's stdlib.
# show.js.haml
- content = capture do
- render file: Rails.root.join('public' , 'js', 'jquery-1.11.2.min.js')
# for remote files
- content = open('http://url.to/file.js').read
You can print the content later on:
= content
I think what you need is the rails asset pipeline
http://guides.rubyonrails.org/asset_pipeline.html
I am currently working on cleaning up a little web app I've written in Mojolicious. As part of that cleanup I am separating out my javascript into the public directory from my html.ep files.
The problem I have though is I don't seem to be able to reference tag helpers any more, like 'url_for' or even referencing values in the stash such as '<% $stashvalue %>'.
Any idea's on how or if I can do this is very much appreciated.
cheers.
The stuff in the public directory is served statically, optimally by your webserver, not by Mojolicious or Plack, so that file does not get processed by Mojolicious, thus <% $stashvalue %> not meaning anything.
A solution would be to embed those parts of javascript that need to access server side variables in the mojo templates - rather ugly, but less code to be written.
Another one would be to make an ajax call from your javascript files, when they are loaded, and get the values sent by the server - more elegant, but more code to be written.
Another one that I can think of, would be to move those javascript files under a folder that gets processed by Mojolicious and include them parameterized - in your html.ep file that needs that js file, do :
<script type="text/javascript" src="http://example.com/url/served/by/mojo/?param1=<% $stashvalue %>¶m2=<% $stashvalue2 %>"></script>
And, in the controller that responds to /url/served/by/mojo/, render that js file, with the params replaced by the ones from the query. As an alternative, you could store/receive those params also on the session
As usually in Perl, there is more than one way to do it.
What I typically do is encapsulate most of my javascript in function calls or objects in pure javascript files. Then in my templates I include those pure javascript files and use the api that I built in those files from the template, interpolating the server-side variables into the arguments of the functions. You can peruse the code for Galileo to see several examples.
For example, see how this template passes stash values to this pure javascript file's functionality.
The way I have it right now is that I use JavaScript to read a file, providing a hard coded path. I tell it to look it /public. I'm going to generate a file in the tmp directory with Rails, and I want to read it with JavaScript. How can I do this? What is the tmp directory of Rails?
I've tried putting the file into /tmp and hard coding JavaScript to read from /tmp, but it doesn't load the file.
I might be wrong but I think browser can access only files inside public folder unless it goes through Rails route. So, you can either change the location of tmp folder or you can create a method in a controller that will read that JS file and send it back to browser (sort of like proxy).
I am newby in Spring, but have a task, and I am learning on the fly.
I used Roo to generates for me part of the code, but now I have to make some dynamic list binding, which is done with form, popping-up in new window, and when the submit button is pushed I have to insert the new values in the parent window.
For the purpose I wrote a .js file, which hooks the values to the parent DOM tree, but the point is that I can't configure Spring to deliver the required .js file to the browser.
The browser, doesn't recognize my function. Even when I try to access the .js file via the browser, I receive error that the file couldn't not be found.
I've tried to configure the web.xml, but it didn't work...
Any ideas, how I can configure the access to a .js file in a Spring MVC application?
Thanks a lot!
P.S. Respectively, I'll need to grant access for a static .htm(l) file... I suppose the principle for configuration of the access of static html files is the same..., right?
You just need to get the path to the file right. Assuming you have a Maven-like set-up (I assume you do because you're using Roo), then your script belongs under src/main/webapp - probably in something like a scripts folder.
Let's assume that your file is at src/main/webapp/scripts/myscript.js
You can create a URL reference for your script by adding the following Spring tag:
<spring:url value="/scripts/myscript.js" var="script_url"/>
This should give you the right path to your script, regardless of the context in which you later decide to publish your webapp.
After that, it's just a matter of using that reference:
<script type="text/javascript" src="${script_url}"></script>
I'd like to split my views in Grails into 2 files a .gsp file and a .js file so that I get a cleaner Javascript separation from my views. So here's an example:
views/index.gsp
views/index.js
views/home/index.jsp
views/home/index.js
But when I simply add the index.js script reference like this:
<script src="index.js" type="text/javascript"></script>
all I get is a 404.
Does anyone knows how to deal with this?
A great benefit would be to have the ability to use view data inside the index.js file to produce the desired content.
Matthias.
Actually, it should be perfectly possible to serve a JS file (or any other file type) as a GSP from your grails-app/views/ directory. The only thing you have to do, is define a suitable URL mapping for those GSPs, e.g.:
"/javascript/home/index"(view:'/home/index.js')
With this URL mapping, you can put your JS code into grails-app/views/home/index.js.gsp (note the trailing .gsp) and you can use any grails tags in your JS source. To ensure that your JS is delivered with the correct content type, you may want to place
<%# page contentType="text/javascript"%>
at the beginning of your GSP.
Unfortunately, the createLink tag doesn't support link rewriting to views, but it should be easy to write your own tag to create those links.
Anyways, keep in mind that this won't have a very positive impact on your app's performance. It's usually better to have static JS files (and also serve them as static resources) while passing dynamic stuff as parameters to JS functions for example. This will also keep you from some headaches wrt. caching etc.
The idea is good, but Grails has this directory structure for a reason. The view folder is intended for a certain artifact type (views)..
You could clone your view folder structure under web-inf, but that gives you more work as I guess the idea behind this is to keep related files close together for convenience reasons.
Even though I'm not to excited about storing Javascript together with the view I loved Robert's idea of hooking into the build process by using build events to copy javascript sources into the right directory! If you decide to go down that road you might as well compress the sources while you're at it. ShrinkSafe is popular library.
I don't think you are allowed to access js inside views/
if you need to do that ... here is the trick
create your js and rename it with myjs.gsp (use "")
iniside _myjs.gsp type you js
... write down you js in here ...
inside you gsp (for example: index.gsp, view.gsp, etc)
type this tag to upload you js
Update 2:
Grails offer the possibility of hooking into the build lifecycle using custom events.
An event handler can be written which synchronises all JavaScript files under grails-app/views with the target folder of web-app/js.
Place the custom code in $PROJECT/scripts/Events.groovy. The PackagingEnd is a good target for the invocation, since it happens right after web.xml is generated.
eventPackagingEnd = { ->
// for each js file under grails-app/views move to web-app/js
}
Update
If you'd like the JavaScript files simply 'meshed' together, you can do that using symlinks, e.g.:
grails-app/views/view1/index.js -> webapp/js/view1/index.js
As far as I know, there is no way of forcing grails to directly serve content which is outside of web-app.
Alternatively, you can inline your JavaScript, but that can have performance implications.
JavaScript files belong under web-app/js.
Then you can reference them using <g:javascript src="index.js" />.