Dynamically creating javascript file in Rails 3.1 - javascript

I am attempting to create a plug and play shopping cart in Rails 3.1 that allows users to add a shopping cart to their site by just adding a link to a javascript file. The items for sale are input on my end and stored in this js file and rendered with jquery templates. I currently have an action that renders the corresponding js, but I was wondering if there was a way to create a new minified js file for each site and link to this file in each site instead of the show action that renders the js.
For example, for store#1, I would like to create and save a js file called store1.js and serve that file instead of calling the show.js action that creates the javascipt array for the jquery templates every time.

You could try using action caching to have it only render the action once. Then you can utilize cache sweepers to invalidate the cached js when you make any updates that would change the information in the js.
I think that really might be your best option. Your going to go through a lot more trouble trying to get precompiled dynamic JS like that, especially if the content has a tendency to change at all.

Related

How to remove a PDF document level Javascript script using either Javascript or Python?

I am currently automating a PDF form creation workflow filling fields and comboboxes using a combination of Python (PyPDF2) and Javascript. I am actually using Python to inserting JS code at the document level for creating comboboxes in forms automatically populated with fresh database data.
The problem I found is, when an user opens the PDF, fills the form then save its combobox choices everything seems ok, but when they reopen the same PDF, those choices are gone because the document scripts reset the comboboxes again because the code run by itself every time it opens.
I am wondering if deleting those document scripts by setting a document action when the document is saved would solve the problem but I couldn't find any Javascript function for this.
If using only Javascript is impossible, can be done using Python? I couldn't find anything as well.
I would consider PDF API method :
this.removeScript("myScript");
But the problem is PyPDF2 generate JS function random names like this:
PS: This solution (partial solution) cannot be applied because my JS code length change constantly (dynamic)

Load HTML from html file into Vue div

So, I'm using jsfiddle to follow THIS
{{respondedText}}
<div>{{respondedText}}</div>
However, say I want to read in HTML content from a file or site and then load it into that div; instead of displaying "Event Received: Event 2".
This is ultimately a building block for me in what I'm trying to use it for. I'm hoping, by successfully getting this example to work, that I can build a webapp that has buttons that, onpress, will load html from another local file on my server without reloading the entire page.
To fill an element with active HTML you have to use the v-html directive
<div v-html="respondedTest"></div>
This will allow any valid HTML but you have to note that you can't load Vue components asynchronously this way; It's only for static HTML.
Here is your JSFiddle Updated to send some HTML with the click events.
EDIT:
Looking into the spirit of your question you might want to look at vue-router It's a pretty good system to allow you to have a single page app with a routing system similar to a standard page routing system. It also allows you to mount Vue components in your pages instead of static HTML.

Loading JavaScript files selectively in Django

I have Post model for a blog app in Django. It has a field named body. In posts, I may use Latex so I need to use MathJax.js. In some posts, I add code snippet, so I use highlight.js. In some I use both, in some I use none of them.
I want to load the relevant javascript depending on the body field of the Post model (similar to THIS). How can I make the relevant .js file(s) to load automatically?
I know that I can add an indicator field like hasLatex (True, False) or hasCode (True, False). But I'm lazy, I want Post.body to be automatically scanned and only relevant js files loaded.
Set something in your context or use a template context processor. For example I load code that handles forms if there is a form key is my context. For something I want on almost every page I put a no_something in my context to disable it. This is done by putting a conditional around the tag in your base template. If the variable is not there or is false it won't show.
What I also do is put my static files in lists inside of my context. JavaScript is in context['js'] and css in context['css']. Those are looped through in my header. I can implement get_context_data in a base class, and all the views that extend from that will have the javascript and css files.

How do I add a global javascript and css to a whole page that uses a predefined template in Adobe CQ5?

I want to Send data via AJax call to serlvet, the serlvet will return some result based to which I will redirect to a page.
So where do I add this global .js file i.e the file that contains the AJAX call. PLEASE NOTE I'M USING PREDEFINED FORM COMPONENT AND A PREDEFINED TEMPLATE(Form Template) SO I CAN'T DO STUFF LIKE <cq:include clientLibs> to add .js file, as I don't have source code of component(form template).
I found a link where we add a global JS but I don't have this option in my CQ5 instance. So Weird.
http://www.sfu.ca/itservices/cms/howto/advanced/style-a-page/customjavascript.html
Am not sure of this custom style-sheet and JavaScript section. Might be they would've customized the page properties as i too don't find them in CQ 5.4 / 5.5.
Though you may not be able to include your custom CSS and JS in JSP using <cq:includeClientLibs>, you can make them as part of the clientlibs that is getting included in the form or the page component.
i.e. You can create a cq:clientLibrary folder that has the categories property set to the same value which is being included in the page.
But, it is recommended to be cautious while trying this way, as your JS would be available in all the pages where this client library is included, and hence try to write the script as specific as possible so that it doesn't affect the rest of the functionality.

Whats the best way to structure my js application, when my views / controllers need to be in the same file

I am Building a learning application where there are a bunch of different page types that a learner will go through and do activities. It will be a SCORM compliant learning object.
This is the structure I have so far...
application/
models/
scorm.js
sequence.js
session.js
pagetypes/
multichoice.js
truefalse.js
basic.js
utilities/
jquery.js
api.js
My pagetypes do the viewing and the controlling, should I seperate these out? The reason I have combined them is so when I build a new page type, I can just drop it into that folder and it will get recognised straight away by the code.
What do you guys think? amidoinrite?
I'm guessing you're separating out methods based on type of page interactions.
I don't see any reason not to do it your way. So long as everything the sco needs is in the manifest you can subdivide your scripts however you want. It might save just a bit of load time to separate out separate page types... But only if you are only loading what you need into the HTML page, & you are actually navigating pages within a sco session. If you're loading all script into a single HTML page, & then dynamically changing the content of page divs, then your scripts are all loaded 1 time & you may as well have 1 minified file for all page type scripts.
I would probably go with the latter, & tie interactions to classes or ids in the markup. 1 file, less work to minify, & I can use in other packages without having to make sure that I have every page type I need...
With JavaScript it can be tricky to separate it out since it lives so closely to the view. As long as the data is separated from the actual view (which it looks like it is in your example) it will be a good design. I would argue that the pagetypes are more controllers and the HTML is the view. The most important part is to keep the model separated from the view. Unless you're trying to build reusable JavaScript/HTML components it's ok for pagetypes to blur the role of controller and view.

Categories