简体   繁体   English

使用 Rails JS 模板的正确方法是什么?

[英]What is the correct way to use Rails JS templates?

It's just not clear to me how to use Rails JS templates well.我只是不清楚如何很好地使用 Rails JS 模板。 They're essentially replacing the success callback on an AJAX request, right?他们实质上是在替换 AJAX 请求的成功回调,对吧? Why would it ever make sense to separate the JS executed when the request is a success from the JS that executes for the various other callbacks?为什么将请求成功时执行的 JS 与为各种其他回调执行的 JS 分开是有意义的?

This of course assumes that I'm binding a click handler or something to a link and making an AJAX request that way.这当然假设我正在将点击处理程序或其他东西绑定到链接并以这种方式发出 AJAX 请求。 I could of course be using a link_to with :remote => true .我当然可以使用带有:remote => truelink_to I suppose in that case it might make sense to use JS templates.我想在这种情况下使用 JS 模板可能是有意义的。 But then what do you do if you need to handle other cases besides success?但是,如果您需要处理除了成功之外的其他情况,您会怎么做? Bind an ajax:failure event to the link generated by the link_to ?ajax:failure事件绑定到由link_to生成的链接? That would mean maintaining JS related to that link in two separate places.这意味着在两个不同的地方维护与该链接相关的 JS。 And what happens if there's two links (say, with different markup) that both make a request to that action, but each one requiring different JS to be executed because they need to behave differently when clicked?如果有两个链接(例如,具有不同标记)都向该操作发出请求,但每个链接都需要执行不同的 JS,因为它们在单击时需要表现不同,会发生什么? How would you handle that with a JS template?您将如何使用 JS 模板来处理它?

Am I thinking about this right?我在想这个吗?

I think the best way is to avoid the js templates.我认为最好的方法是避免使用 js 模板。 It's an aberration to try to write js code in ruby... It's my opinion, but the alternative is much cleaner and simpler because you write the code in the actual language.尝试在 ruby 中编写 js 代码是一种失常......这是我的观点,但替代方案更简洁,因为您使用实际语言编写代码。 So, write your javascript that makes the call to the url provided in your link, and then ask for the content in json.因此,编写您的 javascript 以调用链接中提供的 url,然后询问 json 中的内容。 In your controller, just return the data in json (it can be a list of object with data, or just an object like: {success: true} or {success: false}. Then in your js you treat the object and make the appropiate action. In different parts that point to the same url you can take different actions in your js. In your controller, just return the data in json (it can be a list of object with data, or just an object like: {success: true} or {success: false}. Then in your js you treat the object and make the适当的行动。在指向同一个 url 的不同部分中,您可以在 js 中采取不同的行动。

You would use embedded Ruby (as your javascript templates are js.erb files).您将使用嵌入式 Ruby(因为您的 javascript 模板是 js.erb 文件)。

If you wanted different sets of javascript to be executed depending on success or failure you'd do something like:如果您希望根据成功或失败执行不同的 javascript 集,您可以执行以下操作:

Controller: Controller:

def my_action
  #some code
  @success = true #or false
  respond_to do |format|
    format.js #you may need to add render :layout => false
    format.html #html fallback 
  end
end

Then in my_action.js.erb view:然后在 my_action.js.erb 视图中:

<% if @success %>
  //Some javascript for success
<% else %>
  //Some javascript for failure
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在带有 webpacker 的 Rails 6 中使用 wow.js 的正确方法是什么? - What is the correct way to use wow.js in Rails 6 with webpacker? 在Handlebar模板中的条件下使用数组键的正确方法是什么? - What is a correct way to use array key on if conditions in Handlebar templates? 在 Twig 模板中插入 JavaScript 的正确方法是什么 - What is the correct way to insert JavaScript in Twig templates 在jquery中使用“ this”的正确方法是什么? - What is the correct way to use “this” in jquery? 在 total.js 中,定义我可以在脚本和模板中使用的全局常量的最佳方法是什么? - In total.js what is the best way to define global constants that i can use in scripts and templates? 在vue 3中导入js类的正确方法是什么 - What is the correct way to import a js class in vue 3 访问嵌套的JSON以在Vue.JS组件中使用的正确方法是什么? - What is the correct way to access nested JSON for use in Vue.JS components? 这是使用JS.map的正确方法吗? - Is this the correct way to use JS.map? 在Cordova中使用localForage的正确方法是什么? - What is the correct way to use localForage with Cordova? 使用 javascript .onclick 函数的正确方法是什么? - What is the correct way to use the javascript .onclick function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM