As a follow-up to Project organization and cljsbuild config to require namespace. If I only have one output file, how do I specify what code to run on each separate web page?
One approach might be to include a script tag <script>myproject.somepage.init();</script> in each page. Is there a generally accepted approach?
I think the previous question was interpreted in the context of single page applications where it doesn't make sense to have more than one output file.
If you need different behaviors in separate web pages, I suggest you have an entry namespace for each webpage, using the :main option:
:cljsbuild {:build [{:id "login"
:main my-login.entry
:output-to "resources/public/js/login.js"
:optimizations :advanced}
{:id "feed"
:main my-feed.entry
:output-to "resources/public/js/feed.js"
:optimizations :advanced}]}}
And then require those files on each webpage.
This approach is worth the trouble if my-feed.entry and my-login.entry will require different code, and it would be a waste to deliver all the code for feed also to login. If the behavior is almost the same, and is just a matter of an argument, or one function call, your suggestion is fine.
Related
I would like to collect only the depending partials of a single template. Or otherwise ignore unused partials. Because it is a large project with lots of templates/partials and dependencies it is not possible to do that by hand.
Example file and folder structure would be something like:
/sites/foo.hbs - includes {> partial1}
/sites/bar.hbs - includes {> partial2}
/partials/partial1.hbs
/partials/partial2.hbs
Now I would like to precompile foo.hbs only. For precompilation i have to configure foo.hbs as root and /partials/*.hbs as partials.
The precompilation should end up with the content of foo.hbs and partial1.hbs in a single file.
Is that possible with handlebars at all? Maybe with an existing gulp plugin?
Your question contains a (somewhat) logical error.
Partials were initially intended to act as separate templates.
I guess your intentions are basically more syntactic sugar'ish,
- as you wish to write your code separately though merge it on deployment (some people call that OCD, since it has no real benefit in terms of performance).
Anyhow.
You can run a node scripts that runs through your HBS's, locates partials, extracts them into their parent elements and outputs these final templates.
Take a look at this following answer -
find files by extension, *.html under a folder in nodejs
What is the difference between name.html.erb vs name.erb?
In particular, are there any reasons why name.erb could be bad to use?
I understand that name.html.erb is the convention - this indicates a HTML template and ERB Engine. But I can't find information if are there any reasons not to use name.html.erb, but name.erb instead.
My new workplace asks me to use name.erb, so I want to know: might there be any problems with this?
In short, no, there won't be any problems. Erb files simply output text. In many cases the file extension is ignored by the reading app as the reading app reads/interprets the containing text and its syntax validity. As #taglia suggests, the file extensions are mostly a 'hint' for you and may also be used by the OS to select a default app to open the file with. See here for a more thorough explanation: Output Type for an ERB File
Rails convention dictates template files to include the extension of the output type and the name of the file should end with the .erb extension. As you mentioned, name.html.erb indicates an HTML template and ERB extension that allows any instance variables in your controller's index action to get passed into the template and used. Similarly, name.js.erb indicates a JavaScript template. See here under 'Conventions or Template Files': An Introduction to ERB Templating
ERB is just a templating language, it is not limited to HTML (you could have name.txt.erb, or name.js.erb). Removing html from the name is just going to make your life more difficult (assuming it works), because you won't be able to know what file you are dealing with unless you open it.
I am making a website dedicated to flash and unity games to learn Ruby on Rails hands on and I wanted to know if there was a way to route to all the documents in a folder without entering them in the routes file one by one. I am not sure if I am just being blind, but I have been looking everywhere for an answer.
For example i have to enter:
Get 'game1', to: "games#game1"
In the routes file for an individual game, but I want to be able to add a entire library without having to enter them in one by one.
correct me if i'm wrong, but you have several files (games) into the games view folder and you want to route through a single action?
so, basically, in your routes.rb would have:
get "games/:game_number" => "games#show"
and then, on your games_controller.rb
def show
# ...
render "games/#{params[:game_number]}"
end
so, in this case, if you hit /games/first, it would render the view games/first.html.erb
this is not a good practice since the are several pitfalls through out, but, having that in mind, this should work if i got the right scenario :)
This is what variable segments are for. You should be using something like
get "games/:id" => "games#show"
and looking up the game by params[:id].
I have a project which is not intended to be a single page application, and I'm trying to figure out the best way to share code for common functionality between different script outputs.
For example I may have a stores.js target, and a items.js target built from cljs/stores and cljs/items respectively, but both have some overlapping functionality (say tables with editable cells).
So far the solutions I've come up with are either to symlink common file(s) into both directories that implement the functionality, which I find aesthetically displeasing, but should actually work fine, or to extract the common functionality into a separate package and make the project depend on it. That however involves either requiring developers to setup both repositories and install the dependency locally, or setup a maven server.
This answer from 2012 seems to suggest I should simply do away with multiple targets and instead initialize the functionality I want on the correct page, but it seems to me that having the functionality for the entire site in a single file would make the resulting javascript much larger than it needs to be, increasing the page load time, especially given that I will have more than 2 pages.
Is there a better way?
OK, it turns out the solution is pretty simple, and actually should have been clear from the cljsbuild docs, but I wasn't seeing it. It took trying to reply to #phtrivier for me to actually see what I was missing.
The solution is to use multiple source paths in my project.clj file. So a simple example would look like so:
{:builds [{:source-paths ["src/cljs/items"
"src/cljs/shared"]
:compiler {:output-to "resources/public/js/items.js"
:optimizations :simple
:pretty-print true}
:jar true}
{:source-paths ["src/cljs/stores"
"src/cljs/shared"]
:compiler {:output-to "resources/public/js/stores.js"
:optimizations :simple
:pretty-print true}}]}
from there it's simple:
src/cljs/shared/shared.cljs
(ns shared)
(defn common []
"Hello World")
src/cljs/items/items.cljs
(ns items
(:require [shared :refer [common]]))
(.log js/console (common))
src/cljs/stores/stores.cljs
(ns stores
(:require [shared :refer [common]]))
(js/alert (common))
items.js and stores.js will be their own separate files both containing the shared code.
I'm trying to compile some JS libraries that we have with lein-cljsbuild to integrate them in our ClojureScript code base. First I added some goog.provide in top of each file, and the files are hierarchically organised in a directory tree according to their namespace (like in Java). That is namespace a.b.c is in src-js/libs/a/b/c.js
I have put the JS files in the root directory of the projects in src-js/libs, and I have the following :compiler options for lein-cljsbuild:
{:id "prod",
:source-paths ["src-cljs" "src-js"]
:compiler
{:pretty-print false,
:libs ["libs/"]
:output-to "resources/public/js/compiled-app.js",
:optimizations :simple}}
None of the JS files get compiled into the compiled-app file. What's wrong?
I also tried to put them in resources/closure-js/libs without success.
I'm using lein-cljsbuild 0.3.0.
First, unlike what is suggested in some texts, you do not need to include your private closure library locations in any classpath configuration statement in your project.clj. So unless the "src/js" directory included in your "source-paths:" statement is for some other purpose, you can remove it.
Second, the only thing to add to your project.clj, for the sake of bringing in your private closure code, is the "libs:" reference you have made; BUT unlike what you have entered, that reference must be to a specific *.js file (or files) and not merely a directory. So if the library you want to used is in a file named test.js and that resides in the /src/js directory, your libs: entry would be: "src/js/test.js". See the cljs-build release notes if you want to use that plugin's default :libs directory option.
Third, (and it looks like you know this already, but this is what tripped me up) if you are using a browser-backed REPL (repl-listen option of cljsbuild), you still will not be able to load/reference/use your private library assets from that REPL until you include a :require statement somewhere in the source for your compiled-app.js (e.g. "(ns testing (:require [myprivatelib]))" ), THEN you must re-compile (lein cljsbuild once) and reload your browser page with a link to compiled-app.js. This brings in that code base. Otherwise, your browser REPL will just keep insisting that the namespace provided in your closure library is not defined.
I hope this helps.