I have a Rails 3 application that is serving a js file e.g., tools.js.erb via the assets pipeline. This is working fine, but I want to add some html templates which are available as partials within the environment.
我有一个Rails 3应用程序,通过资产管道服务js文件,例如tools.js.erb。这工作正常,但我想添加一些html模板,这些模板在环境中可用作部分。
One option was to load the template via an ajax call from the JS when the template is required, but that would add some delay in presenting it on the client.
一种选择是在需要模板时通过JS的ajax调用加载模板,但这会在客户端上呈现它时会增加一些延迟。
Is it possible to render a partial inside the JS and store it in a JS variable as a string, so I can just inject the html right away. What is the best way to do this?
是否可以在JS中呈现部分内容并将其作为字符串存储在JS变量中,因此我可以立即注入html。做这个的最好方式是什么?
3 个解决方案
#1
0
Yeah, you can do something like this:
是的,你可以这样做:
var container = $("#your_container");
var myPartial = "<%= j render( :partial => 'directory/your_partial' ) %>";
// on.click, or whatever...
container.prepend(myPartial);
Not sure if you want to prepend or replace the HTML, but this should get you on the right track. Good call saving the markup for the partial in a Javascript variable. Should speed things up.
不确定您是否要预先添加或替换HTML,但这应该让您走上正确的轨道。好的调用在Javascript变量中保存部分标记。应该加快速度。
#2
0
It HTML you want to add/update requires some variables, you would have to make a server trip anyways and would have reload your dom. Otherwise if it is a static content, you can follow this approach. Just create one variable in your action like this.
你要添加/更新的HTML需要一些变量,你必须让服务器旅行,并且会重新加载你的dom。否则,如果它是静态内容,您可以遵循此方法。只需在您的操作中创建一个变量,就像这样。
@my_view_content = render_to_string partial: 'path_to_partial', formats: ['html'], layout: false, object: something
Now, you can use this variable or pass this variable to your JS file.
现在,您可以使用此变量或将此变量传递给JS文件。
var my_view_content_string = = "#{@my_view_content}"
#3
0
Why do you want to store a view in a variable if you have few good options..why cant you use a conditon..such as in show.js.erb
如果你没有什么好的选择,为什么你想在一个变量中存储一个视图..为什么你不能使用条件......如show.js.erb
<% if condition %>
$('#div1').html("<%= escape_javascript(render(:partial => 'pages/show')) %>");
<%else%>
$('#div1').html("<%= escape_javascript(render(:partial => 'users/show')) %>");
<%end%>
ELSE THIS IS OTHER WAY TO DO IT....
这是其他方式......
###check of a condition in your view and then add element and call ajax
<%if current_user.sign_in_count == 1 && current_user.confirmed? %>
<%= hidden_field(:new_user, :first_login, :value => current_user.sign_in_count)%>
<%end%>
<script type="text/javascript">
$(document).ready(function() {
//check if element is present in the dom and then call ajax
if ($('#new_user_first_login').length) {
$.ajax({
//CALL AJAX
})
}
})//document ends
</script>
#1
0
Yeah, you can do something like this:
是的,你可以这样做:
var container = $("#your_container");
var myPartial = "<%= j render( :partial => 'directory/your_partial' ) %>";
// on.click, or whatever...
container.prepend(myPartial);
Not sure if you want to prepend or replace the HTML, but this should get you on the right track. Good call saving the markup for the partial in a Javascript variable. Should speed things up.
不确定您是否要预先添加或替换HTML,但这应该让您走上正确的轨道。好的调用在Javascript变量中保存部分标记。应该加快速度。
#2
0
It HTML you want to add/update requires some variables, you would have to make a server trip anyways and would have reload your dom. Otherwise if it is a static content, you can follow this approach. Just create one variable in your action like this.
你要添加/更新的HTML需要一些变量,你必须让服务器旅行,并且会重新加载你的dom。否则,如果它是静态内容,您可以遵循此方法。只需在您的操作中创建一个变量,就像这样。
@my_view_content = render_to_string partial: 'path_to_partial', formats: ['html'], layout: false, object: something
Now, you can use this variable or pass this variable to your JS file.
现在,您可以使用此变量或将此变量传递给JS文件。
var my_view_content_string = = "#{@my_view_content}"
#3
0
Why do you want to store a view in a variable if you have few good options..why cant you use a conditon..such as in show.js.erb
如果你没有什么好的选择,为什么你想在一个变量中存储一个视图..为什么你不能使用条件......如show.js.erb
<% if condition %>
$('#div1').html("<%= escape_javascript(render(:partial => 'pages/show')) %>");
<%else%>
$('#div1').html("<%= escape_javascript(render(:partial => 'users/show')) %>");
<%end%>
ELSE THIS IS OTHER WAY TO DO IT....
这是其他方式......
###check of a condition in your view and then add element and call ajax
<%if current_user.sign_in_count == 1 && current_user.confirmed? %>
<%= hidden_field(:new_user, :first_login, :value => current_user.sign_in_count)%>
<%end%>
<script type="text/javascript">
$(document).ready(function() {
//check if element is present in the dom and then call ajax
if ($('#new_user_first_login').length) {
$.ajax({
//CALL AJAX
})
}
})//document ends
</script>