$(function)在Rails 3.2.1 App中被调用两次 [英] $(function) gets called twice in Rails 3.2.1 App

查看:115
本文介绍了$(function)在Rails 3.2.1 App中被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a Rails 3.2.1 app that I'm trying to include some jquery in.

以下是文件:

states.js:

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:

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

我得到了嗨"每次页面加载时都会弹出两次.

I'm getting the "hi" popup twice on every page load.

由于某种原因有2个请求出去吗?

Are there 2 requests going out for some reason?

谢谢

结果是,states.js被两次加载到DOM中.我没有预编译资产.还有其他想法为什么文件要加载两次?

Turns out states.js is being loaded to the DOM twice. I'm not precompiling the assets. Any other ideas why the file is getting loaded twice?

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,它就可以正常工作.

I'm guessing the states.js.coffee was loading the file as well as application.js. As soon as I removed states.js.coffee, it worked fine.

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

Know where I might read up on *.coffee?

application.js:

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

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,它就可以正常工作.

I'm guessing the states.js.coffee was loading the file as well as application.js. As soon as I removed states.js.coffee, it worked fine.

这篇关于$(function)在Rails 3.2.1 App中被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆