[英]How to initialize a variable with javascript in ruby on rails?
I am trying to implement a great theme to a rails 4 app for learning purposes. 我正在尝试为Rails 4应用程序实现一个伟大的主题,以达到学习目的。 The theme located here: http://tympanus.net/codrops/2012/09/25/3d-restaurant-menu-concept/ 主题位于此处: http : //tympanus.net/codrops/2012/09/25/3d-restaurant-menu-concept/
I distributed the .css and .js in their respective directories. 我在各自的目录中分发了.css和.js。
vendor/assets/stylesheets/
vendor/assets/javascripts/
I added the new files in the assets pipeline for the app to load them 我在资产管道中添加了新文件以供应用加载
CSS CSS
app/assets/stylesheets/application.css
*= require style
*= require normalize
*= require demo
*= require_tree .
*= require_self
Javascript files are loading, I can see it on the header of the page source. Javascript文件正在加载,我可以在页面源头上看到它。
Here is the javascript file: 这是JavaScript文件:
var Menu = (function() {
var $container = $( '#rm-container' ),
$cover = $container.find( 'div.rm-cover' ),
$middle = $container.find( 'div.rm-middle' ),
$right = $container.find( 'div.rm-right' ),
$open = $cover.find('a.rm-button-open'),
$close = $right.find('span.rm-close'),
$details = $container.find( 'a.rm-viewdetails' ),
init = function() {
initEvents();
},
initEvents = function() {
$open.on( 'click', function( event ) {
openMenu();
return false;
} );
$close.on( 'click', function( event ) {
closeMenu();
return false;
} );
$details.on( 'click', function( event ) {
$container.removeClass( 'rm-in' ).children( 'div.rm-modal' ).remove();
viewDetails( $( this ) );
return false;
} );
},
openMenu = function() {
$container.addClass( 'rm-open' );
},
closeMenu = function() {
$container.removeClass( 'rm-open rm-nodelay rm-in' );
},
viewDetails = function( recipe ) {
var title = recipe.text(),
img = recipe.data( 'thumb' ),
description = recipe.parent().next().text(),
url = recipe.attr( 'href' );
var $modal = $( '<div class="rm-modal"><div class="rm-thumb" style="background-image: url(' + img + ')"></div><h5>' + title + '</h5><p>' + description + '</p><a href="' + url + '">See the recipe</a><span class="rm-close-modal">x</span></div>' );
$modal.appendTo( $container );
var h = $modal.outerHeight( true );
$modal.css( 'margin-top', -h / 2 );
setTimeout( function() {
$container.addClass( 'rm-in rm-nodelay' );
$modal.find( 'span.rm-close-modal' ).on( 'click', function() {
$container.removeClass( 'rm-in' );
} );
}, 0 );
};
return { init : init };
})();
There is a script in the html file I need to run but I have not been able to implement it to rails 4.2. 我需要运行html文件中的一个脚本,但无法将其实现到rails 4.2。
<script type="text/javascript">
$(function() {
Menu.init();
});
</script>
I ve tried to leave the script in the .html.erb file but it does not work, I also tried to add the script to a javascript file in app/assets/javascripts/controller.js but is does not work either. 我尝试将脚本保留在.html.erb文件中,但它不起作用,我还尝试将脚本添加到app / assets / javascripts / controller.js中的javascript文件中,但也不起作用。
This works if I click on the html file of the template in my computer, but how can I implement it on rails? 如果我在计算机上单击模板的html文件,这将起作用,但是如何在Rails上实现它呢?
For this you could use this in your view file 为此,您可以在视图文件中使用它
<script>
$.getScript('/path to file', function(){
$(document).ready(menu);
$(document).on('page:load', menu);
});
</script>
If the document is loaded the Menu function will be executed. 如果加载了文档,将执行菜单功能。
Its is possible that the $.getScript()
can't find the function, there for you will need to put the js file in the views map of the specific controller. $.getScript()
可能找不到该函数,因为那里您需要将js文件放入特定控制器的视图映射中。
Don't forget to add a route in the config/routes.rb
不要忘记在config/routes.rb
添加路由
And change var Menu = (function() {
into menu = function() {
and place var menu;
in the top of the js file. 然后将var Menu = (function() {
更改为menu = function() {
并将var menu;
放置在js文件的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.