I'm trying to integrate a tool that I have developed in JS using kineticjs in a ruby on rails application. But I'm facing with a problem. Where should I put the kineticjs framework?
At the moment I have done this:
But I'm always getting this:
Uncaught ReferenceError: Kinetic is not defined
How should I include the kineticjs framework?
Edit:
Application.js looks like this:
//= require jquery
//= require bootstrap-sprockets
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
drawingcanvas.js refers to kineticjs:
var rect = new Kinetic.Rect({
x: 0,
y: PALETTE_HEIGHT,
...
I use the container id of a canvas element to do this:
var stage = new Kinetic.Stage({
container: 'container',
width: CANVAS_WIDTH,
height: CANVAS_HEIGHT
});
Taking some guesses:
Am assuming that you are using the default //= require_tree . as the last line in application.js.
Your drawingcanvas.js refers to kineticjs.
somehow the default or your setting requires an element with id='container' to be present in the DOM.
Looks to me like there are multiple issues, if the above are correct.
The require_tree loads the javascripts in an order, and drawingcanvas.js is loaded before the dependencies are loaded. You also need to setup the dependencies for Kineticjs, example set the right container that it can bind to.
To avoid the order of loading affect the dependency, you can wait for the document.onload event, or $(document).ready if you are using jquery and then do the bindings.
EDIT 1:
Here is an example inn Rails that I put together, based on the html example on KineticJs website.
Things to look at:
routes.rb - The root is mapped to Home controller, Index action.
home.js - This is where the javascript logic to generate the Tango example goes.
home.html.erb - Include page specific javascript block, to include reference to kinetic.js.
See it in action by running it.
Related
I use Rails 5.1 and have a javascript file in app/assets/javascripts vex.combined.js and how can I require it within application.js?
//= require jquery
//= require rails-ujs
//= require_tree .
var vex = require('vex.combined');
returns an error in browser console: ReferenceError: require is not defined
Update:
By using //= require vex.combined
How can I transform vex.registerPlugin(require('vex-dialog')); ?
It returns
ReferenceError: require is not defined
too.
Look, you don't need to use this var vex = require('vex.combined');
Rails Asset Pipeline:
The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass, and ERB.
For example
When we include a file using application.js like //= require select2 then we access to this all methods like after requiring this then we just called the method like select2();
$("#ID").select2();
when we remove this //= require select2 from application.js then it will show
ReferenceError: select2 is not a function
Try the following.
For the most common usage of vex, you'll want to include vex, vex-dialog, the vex CSS file, and a theme file.
//= require vex.combined
make sure directory is ok, then try to run basic alert like
vex.dialog.alert('Thanks for checking out vex!')
that's actually ok.
var vex = require('vex-js')
vex.registerPlugin(require('vex-dialog'))
Above two lines code for browserify/webpack setup which you are trying.
I am trying to install a plugin called Chaffel.js, I have added it to my javascript file
+app
|+assets
||+javacripts
|||chaffle.min.js
|||
I have required it in application.js
//= require chaffle.min.js
//= require rails-ujs
//= require turbolinks
//= require_tree .
And I have also added this to config/initializers/assets.rb
Rails.application.config.assets.precompile += %w(chaffle.min.js)
And I have included it in my view
<%= javascript_include_tag 'chaffel.min' %>
And have added all the html / javascript it showed for the example but when I load up the view it gives me this error
Sprockets::Rails::Helper::AssetNotFound in Home#index
The asset "chaffel.min.js" is not present in the asset pipeline.
I don't know if it is a problem with my asset pipeline or if the plugin just doesn't work anymore (As the cdn src on its page doesn't seem to work either) Would love some help with this or recommendations for a different plugin/way to achieve the same effect (a text shuffle animation).
I was able to get it working in a normal html file by just directly including the file via
<script src="chaffle.min.js" charset="utf-8"></script>
So it is definitely a problem with my code / the asset pipeline and not the plugin.
First of all, if you have //= require_tree . in your application.js you don't need to change any other files. //= require_tree . will include all files in the javascript directory for you:
http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
But the problem you have faced, I guess is a just a typo: chaffle / chaffel. But again, just clear all mentions of it and leave only require_tree – that should be enough.
I got inspiered by Makenzie Child video and github repo to create a masonry effect on my rails app.
I have a little problem, when I visit the page, the behavior is not the expected one, all the boxes are below each others, and if I reload they take the good behavior... I would like that the good behavior could display when we arrive on the page.
I don't know any javascript please dont be mad at me :)
this is the progresses.js.coffee
$ ->
$('#progresses').imagesLoaded ->
$('#progresses').masonry
itemSelector: '.box'
isFitWidth: true
application.js
//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require masonry/jquery.masonry
//= require turbolinks
//= require_tree .
views/progresses/index.html.slim
.container
.section
.row
.col-xs-12
h1.text-gold
|Work In Progress [
= #progresses.count
| ]
hr
#progresses.transitions-enabled
- #progresses.each do |progress|
.box.panel.panel-default
= link_to (image_tag progress.main_image.url), progress
.panel-body
h4.text-gold = progress.title.upcase
h5 = progress.date
small = truncate(progress.content, length: 150)
Thanks for your help,
regards
The fact that it works when you reload the page makes me suspect this is a turbolinks problem.
Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body and the title in the head.
So you'll visit the page using a link within your application, the javascript doesn't reload. But when you do a refresh on the page, everything is fetched again including the javascript causing the script to execute.
Solution 1
This also means things like $(document).ready(function(){ won't work because this only fires on initial load. Try something like this out:
ready = ->
$('#progresses').imagesLoaded ->
$('#progresses').masonry
itemSelector: '.box'
isFitWidth: true
$(document).on('page:load', ready)
I'm assuming you're using rails 4, this won't work in rails 5. I've used the following question for information and there are ways described to do this in rails 5: Rails 4: how to use $(document).ready() with turbo-links
Turbolinks will trigger the page:load event when a new page is loaded.
Solution 2
Another solution might be using jquery-turbolinks gem.
It's easy to use. Add gem 'jquery-turbolinks' to your gemfile. Then add //= require jquery.turbolinks to your application.js. This has to go between //= require jquery and //= require jquery_ujs. So in your case:
//= require jquery
//= require jquery.turbolinks
//= require bootstrap-sprockets
//= require jquery_ujs
//= require masonry/jquery.masonry
//= require turbolinks
//= require_tree .
If any of this doesn't work, you might want to disable turbolinks for a moment to see if turbolinks is actually the problem.
You can do this by replacing the <body> tag in your application.html.erb with the following: <body data-no-turbolink="true"> Just note that disabling turbolinks for your whole application isn't something you'll want. There are clean solutions to disable it for a specific page.
EDIT
If the following solution works for you, you can try this instead of the code I gave in solution 1:
$(document).on('turbolinks:load', function() {
...your javascript goes here...
});
In my opinion this is a lot cleaner and I've read that it should work from rails 4.2 and above. Notice the difference: turbolinks:load instead of page:load.
So I go it to work with
ready = ->
$('#progresses').imagesLoaded ->
$('#progresses').masonry
itemSelector: '.box'
isFitWidth: true
$(document).on('turbolinks:load', ready)
As I mentionned in my first comment my boxes were overclapped because I missed the indentation on the line $('#progresses').masonry
Now It works perfectly
Thanks again for all
I'm using RoR and jquery.ui.addresspicker.js
This jquery.ui.addresspicker.js requires that google is loaded before that library is loaded. I only need the addresspicker on a couple of pages in the application.
The google part that needs to be loaded before is this:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
But this makes the page load slower so I don't want this on all my pages.
The application.js contains this:
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require jquery.blockUI
//= require select2
//= require cocoon
//= require_tree .
Currently the jquery.ui.addresspicker.js file is in app/assets/javascripts and thus also loaded automatically all the time.
How can I handle this? Should I move the addresspicker.js out of the javascripts directory? Should I rewrite my application.js? Other suggestions?
You can replace
//= require_tree .
with an explicit list of the javascripts that you want included on each page. and just include jquery.ui.addresspicker on the pages that you need it.
I used Ian's idea to change the application.js
I created a new folder named sitewide and copied all the javascripts in there that I want to use on the whole site (so not the addresspicker).
I changed application.js like this:
//= require_tree ./sitewide
And in the page where I needed the addresspicker I put it using javascript_include_tag
This way I don't need to change the application.js file everytime I want to add a js file.
use a different layout on both pages or just remove the jquery.ui.addresspicker.js from the default_layout and just include in the pages where you need it
<%= include_javascripts "jquery.ui.addresspicker" %> or
javascript_include_tag "jquery.ui.addresspicker"
in the page where you need
I have a Rails application, and I'm using Ember on the front-end. I'd like to move the ember-related files down one level in the directory structure, but when I do, the templates no longer render.
In the plain, vanilla, working version of the application, my directory structure is:
./app/
assets/
javascripts
application.js
ember-app.js
routes.js
store.js
models/
controllers/
routes/
templates/
views/
with: application.js
//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require ember-app
App = Ember.Application.create();
and: ember-app.js
//= require ./store
//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./views
//= require_tree ./helpers
//= require_tree ./templates
//= require ./router
//= require_tree ./routes
Everything works fine. However, I would like to move the ember-app file and all ember javascript code down one level, and when I do so, the templates do not render. (Part of the application uses Ember, but not the entire application, and I'm trying to set up two separate paths through the asset pipeline.)
The desired structure is:
./app/
assets/
javascripts
application.js
embro/
ember-app.js
routes.js
store.js
models/
controllers/
routes/
templates/
views/
with: application.js (revised: 'require ember-app' becomes 'require embro/ember-app')
//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require embro/ember-app
App = Ember.Application.create();
(ember-app.js is unrevised.)
As I said, after the move, none of the template content appears onscreen. No errors onscreen or in the console, just an empty ember-application.
When I examine Ember.TEMPLATES in the console, all the expected templates are listed. Furthermore, if I put the desired content in x-handlebars templates in the appropriate rails view, the content successfully renders, just as it did with the original directory structure.
For example, in apps/views/welcome/index.html....
<script type="text/x-handlebars" data-template-name="application">
<h1>hello</h1>
{{ outlet }}
</script>
<script type="text/x-handlebars" data-template-name="index">
<h1>this is the index</h1>
</script>
... and we're good to go again.
but if I leave the rails view empty, as I did with the original structure, it's a no go.
Wondering if perhaps the ember-rails gem requires the handlebars templates to be present in app/assets/javascripts/templates, and if there's a way to override this. The documentation mentions adding a templates_root option to the application configuration block, and I'm wondering if this is the key. I've played around a bit, no luck yet.
Any ideas?
UPDATE:
Afraid I'm not having any luck with the templates_root option. As an experiment, I tried building a new, simple rails app, and using the ember-rails bootstrap generator to get it up and running. All's well, but if I then attempt to simply change the name of the templates folder (i.e. app/assets/javascripts/templates -> app/assets/javascripts/temple), with appropriate changes to the sprockets includes and config files, I'm getting the same results.
Any chance the templates_root option is somehow broken?
I'm using Ruby 1.9.3, Rails 3.2.11, ember-rails 0.10.0
Any pointers to where I should look in the ember / ember-rails / handlebars source code? Have started poking around.
thanks!
You're right that you need to set templates_root. Try adding
config.handlebars.templates_root = 'embro/templates'
to the configuration block in application.rb, or
RailsApp::Application.config.handlebars.templates_root = 'embro/templates/'
to a new initializer, where RailsApp is whatever your application is named.
Edit:
I was able to reproduce the behaviour that you described with templates_root. The fix for me was to delete the /tmp folder of my application and restart rails. After that, the templates were named correctly.
Edit:
More precisely, you need to clear the sprockets cache at /tmp/cache/assets after changing templates_root.
Edit:
As mentioned in the comments below, a simple rake tmp:cache:clear should take care of the problem.