Javascript 预编译文件工作不正常
我正在开发一个关于传单的项目。我有一个javascript文件,它必须更新公司的坐标。所以,当我在developmentenv,rails使用map_update.coffee.erb时,一切都很顺利。当我试图使rake资产:预编译并在productionenv中启动本地服务器时,map就被破坏了。它正在工作,但与开发环境完全不同 我的js文件是预编译的(我可以在public/assets/map中看到),因为我在production.rb中显式地预编译了它。但效果不好 任何想法都会很棒!如果你需要我的一些文件,只要问我,我会提供所需的代码,我已经写了。谢谢 编辑 production.rb:Javascript 预编译文件工作不正常,javascript,ruby-on-rails,asset-pipeline,production,precompile,Javascript,Ruby On Rails,Asset Pipeline,Production,Precompile,我正在开发一个关于传单的项目。我有一个javascript文件,它必须更新公司的坐标。所以,当我在developmentenv,rails使用map_update.coffee.erb时,一切都很顺利。当我试图使rake资产:预编译并在productionenv中启动本地服务器时,map就被破坏了。它正在工作,但与开发环境完全不同 我的js文件是预编译的(我可以在public/assets/map中看到),因为我在production.rb中显式地预编译了它。但效果不好 任何想法都会很棒!如果你
Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
require 'syslog/logger'
config.logger = Syslog::Logger.new 'abw'
config.logger.level = 2
config.assets.precompile += %w( application-ie.css application-ie.js )
config.assets.precompile += %w( abwp.css.scss abwp.css xgemius.js )
config.assets.precompile += %w( map/map.js map/map_update.js )
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
map_update.coffee.erb正在开发环境中工作,预编译后不在生产中:
#= require map/map.coffee.erb
jQuery $ ->
coord = $('#coordinates-val').val().replace('[',"").replace(']',"").split(",").map(Number)
loc = [coord[1], coord[0]]
map.setCompanyLocation loc
map.layers.companyLocationMarker.on 'dragend', (e) ->
$('#coordinates-val').val(e.target._latlng.lng + ", " + e.target._latlng.lat)
$("#location-detector-update").on 'click', (e) ->
e.preventDefault()
address = $("#location-address").val()
errorElement = $("#location-error")
if address.length > 0
$.ajax
url: 'http://geocode-maps.yandex.ru/1.x/'
type: 'get'
data:
geocode: address
format: 'json'
results: 1
,success: (data) =>
if typeof(data) == 'string'
data = JSON.parse(data)
if data.response.GeoObjectCollection.featureMember.length > 0
_location = data.response.GeoObjectCollection.featureMember[0].GeoObject.Point.pos.split(" ")
_location[0] = 0 if _location[0] == null
_location[1] = 0 if _location[1] == null
location = new L.LatLng(_location[1], _location[0])
map.setCompanyLocation location
$('#coordinates-val').val(_location[0] + ", " + _location[1])
map.layers.companyLocationMarker.on 'dragend', (e) ->
$('#coordinates-val').val(e.target._latlng.lng + ", " + e.target._latlng.lat)
errorElement.html ""
else
errorElement.html "Не найдено"
“不工作”意味着,当我在运行dev env时更新坐标的页面上时,一切都如我所期望的那样工作:如果公司有坐标,它会显示坐标,并提供输入地址的机会,并向db发布另一个坐标。若公司并没有坐标,地图将在自定义的默认位置初始化。在production env中,我只看到标记,没有地图图片。仍然可以输入地址查找坐标并将其发布到db,但我看不到地图本身,因此无法将标记移动到正确的位置。啊,忘了。Cath认为:
#map
.map-filter
#location-error
.input-append.adress-input
= text_field_tag 'location-address', nil, class: 'input', placeholder: 'Enter your adress'
= link_to nil, class: 'btn', id: 'location-detector-update' do
i.icon-search
= javascript_include_tag 'map/map_update'
= render 'form_coordinates'
希望这就足够开始了
还有一件事。我有另一种观点,只是想看看公司的地图,我可以做到这一点。因此,映射正在正确初始化。正如我所悲伤的,我的map_update.coffee.erb的预编译出现了问题,因此,我终于找到了问题所在 在我的更新中,我需要使用它自己的window.map实例化。这不适用于这种改变标记的情况。我重写了我的update_map.js,现在它使用自己的window.map实例。这是我的更新_map.js:
ACTIVEOBJECTICON = L.icon
iconUrl: '<%= image_path("green-marker.png") %>',
iconSize: [25, 41],
iconAnchor: [13, 41]
draggable: true
class EditMap extends L.Map
constructor: (id, params) ->
super id, params
@layers =
companyLocationMarker: new L.LayerGroup()
activeObjectMarker: new L.LayerGroup()
@activeObjectIcon = ACTIVEOBJECTICON
@putInit()
putInit: =>
if coord == "[0]" || coord == ""
alert("You didn't setup adress.")
else
latLng = coord.replace('[',"").replace(']',"").split(",").map(Number)
val1 = latLng[1]
val0 = latLng[0]
location = new L.LatLng(val1, val0)
@layers.activeObjectMarker = new L.Marker(location, { icon: @activeObjectIcon} )
@addLayer(@layers.activeObjectMarker)
@layers.activeObjectMarker.dragging.enable()
@setView(location, 15)
@panTo location
setCompanyLocation: (location) =>
@currentLocation = location
map.renderCompanyLocation()
renderCompanyLocation: =>
locationIcon = ACTIVEOBJECTICON
@removeLayer(@layers.activeObjectMarker)
@removeLayer(@layers.companyLocationMarker)
@layers.companyLocationMarker = new L.Marker(@currentLocation, { icon: locationIcon })
@addLayer(@layers.companyLocationMarker)
@layers.companyLocationMarker.dragging.enable()
@setView(@currentLocation, 15)
jQuery $ ->
osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
osmTiles = new L.TileLayer osmUrl,
maxZoom: 18
window.map = new EditMap 'map',
attributionControl: false,
zoom: 12,
doubleClickZoom: false
center: new L.LatLng(53.9060, 27.5549)
map.addLayer osmTiles
$("#location-detector-update").on 'click', (e) ->
e.preventDefault()
address = $("#location-address").val()
errorElement = $("#location-error")
if address.length > 0
$.ajax
url: 'http://geocode-maps.yandex.ru/1.x/'
type: 'get'
data:
geocode: address
format: 'json'
results: 1
,success: (data) =>
if typeof(data) == 'string'
data = JSON.parse(data)
if data.response.GeoObjectCollection.featureMember.length > 0
_location = data.response.GeoObjectCollection.featureMember[0].GeoObject.Point.pos.split(" ")
_location[0] = 0 if _location[0] == null
_location[1] = 0 if _location[1] == null
location = new L.LatLng(_location[1], _location[0])
map.setCompanyLocation location
$('#coordinates-val').val(_location[0] + ", " + _location[1])
map.layers.companyLocationMarker.on 'dragend', (e) ->
$('#coordinates-val').val(e.target._latlng.lng + ", " + e.target._latlng.lat)
errorElement.html ""
else
errorElement.html "Unable to find"
ACTIVEOBJECTICON=L.icon
iconUrl:“”,
iconSize:[25,41],
iconAnchor:[13,41]
德拉格布尔:是的
类EditMap扩展了L.Map
构造函数:(id,params)->
超级id,参数
@层=
companyLocationMarker:新的L.LayerGroup()
activeObjectMarker:新的L.LayerGroup()
@activeObjectIcon=activeObjectIcon
@putInit()
putInit:=>
如果坐标=“[0]”| |坐标=“”
警报(“您没有设置地址。”)
其他的
latLng=coord.replace('[','').replace(']','').split(“,”).map(编号)
val1=板条[1]
val0=latLng[0]
位置=新L.LatLng(val1,val0)
@layers.activeObjectMarker=新的L.Marker(位置,{icon:@activeobjectcon})
@addLayer(@layers.activeObjectMarker)
@layers.activeObjectMarker.Draging.enable()文件
@setView(位置,15)
@潘托位置
setCompanyLocation:(位置)=>
@当前位置=位置
map.renderCompanyLocation()
renderCompanyLocation:=>
locationIcon=ActiveObjectCon
@removeLayer(@layers.activeObjectMarker)
@removeLayer(@layers.companyLocationMarker)
@layers.companyLocationMarker=新的L.Marker(@currentLocation,{icon:locationIcon})
@addLayer(@layers.companyLocationMarker)
@layers.companyLocationMarker.dragging.enable()文件
@setView(@currentLocation,15)
jQuery$->
osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
osmTiles=新的L.Tillelayer osmUrl,
最大缩放:18
window.map=new EditMap'map',
属性控制:false,
缩放:12,
双击缩放:false
中心:新拉特林(53.9060,27.5549)
map.addLayer osmTiles
$(“#位置检测器更新”)。单击(e)->
e、 预防默认值()
地址=$(“#位置地址”).val()
errorElement=$(“#位置错误”)
如果address.length>0
$.ajax
网址:'http://geocode-maps.yandex.ru/1.x/'
键入:“获取”
数据:
地理编码:地址
格式:“json”
结果:1
,成功:(数据)=>
如果类型(数据)=“字符串”
data=JSON.parse(数据)
如果data.response.GeoObjectCollection.featureMember.length>0
_location=data.response.GeoObjectCollection.featureMember[0].GeoObject.Point.pos.split(“”)
_位置[0]=0如果位置[0]=null
_如果位置[1]=null,则位置[1]=0
位置=新的L.LatLng(_位置[1],_位置[0])
map.setCompanyLocation位置
$(“#坐标val”).val(_位置[0]+”,“+_位置[1])
map.layers.companyLocationMarker.on'dragend',(e)->
$(“#坐标val').val(e.target._latlng.lng+”,“+e.target._latlng.lat)
errorElement.html“”
其他的
errorElement.html“无法找到”
production.rb是一样的。但它不好用。那太糟糕了。在没有代码和问题的明确描述的情况下,你到底希望谁来帮助你解决这个问题?问题似乎是在预编译中,而不是在我的js文件中。如果您需要一些特定的代码,比如my production.rb,我可以提供。我没有给出任何代码,因为我不知道为什么js可以在开发环境中工作,而不能在生产环境中工作。所以,告诉我你需要什么代码,你就会得到它。看看你的问题,问问自己,如果有人问你这个问题,你会从哪里开始,没有其他信息。如果你需要的话,不要犹豫问任何其他问题