gpt4 book ai didi

jquery - $(function) 在 Rails 3.2.1 App 中被调用两次

转载 作者:行者123 更新时间:2023-12-01 01:18:02 26 4
gpt4 key购买 nike

我有一个 Rails 3.2.1 应用程序,我试图在其中包含一些 jquery。

以下是文件:

states.js:

var grid;

var columns = [
{id: "firstName", name: "FirstName", field: "FirstName"},
{id: "lastName", name: "LastName", field: "LastName"}
]



var options = {
enableCellNavigation: true,
enableColumnReorder: false
};

$(function () {
alert("hi");
var state = $("#myGrid").data("state");
$.getJSON(state, function(data){
grid = new Slick.Grid("#myGrid", data, columns, options);
});
})

states_controller.rb:

class StatesController < ApplicationController
def index
@states = SiteIndexPlayer.select("DISTINCT HometownState")
end

def name
logger.debug(":name #{params[:name]}")
@athletes = SiteIndexPlayer.select('FirstName, LastName').find_all_by_HometownState(params[:name])
@state=params[:name]
respond_to do |format|
format.html
format.json { render json: @athletes }
end
end
end

每次页面加载时,我都会收到两次“hi”弹出窗口。

是否有 2 个请求因某种原因发出?

谢谢

问题

结果 states.js 被加载到 DOM 两次。我没有预编译 Assets 。关于文件加载两次的任何其他想法?

解决方案

ls app/assets/javascripts
application.js lib slick.core.js slick.grid.js states.js.coffee states.js

我猜 states.js.coffee 正在加载该文件以及 application.js。当我删除 states.js.coffee 后,它就工作得很好。

知道我可以在哪里阅读*.coffee吗?

编辑

application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .

application.html.erb

<!DOCTYPE html>
<html>
<head>
<title>CapApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

最佳答案

我猜测 states.js.coffee 正在加载该文件以及 application.js。当我删除 states.js.coffee 后,它就正常工作了。

关于jquery - $(function) 在 Rails 3.2.1 App 中被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9405301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com