Tag: coffeescript

为什么javascript“this”不适用于“each”?

我正在尝试在检测到它具有”true”的data-voted属性时为一个元素分配一个类,但是简单的addClass行不起作用。 我使用$(this)直到我为each()读取jQuery的文档,然后切换到以下内容: windowReady = -> jQuery -> $voteLinks = $(‘.vote-button a’) $voteLinks.each (i, current) -> if $(current).data(‘voted’) == “true” $(current).addClass(‘voted’) $(window).load(windowReady); $(window).on(‘page:load’, windowReady); $(this) windowReady = -> jQuery -> $voteLinks = $(‘.vote-button a’) $voteLinks.each -> if $(this).data(‘voted’) == “true” $(this).addClass(‘voted’) $(window).load(windowReady); $(window).on(‘page:load’, windowReady); 但即便如此我仍然没有指定一个类,即使我已经确认它确实具有”true”的data-voted属性 CoffeesSript可以在js2coffee转换为Javascript。

jQuery“对象函数(e,t){return new v.fn.init(e,t,n)}没有方法’on’”

我正在关注@coreyward的例子, 它将对象编辑表单放在Rails中的modal dialog窗口中,这在本答案中有所概述,并在此要点中针对rails 3.1进行了更新 。 我正在使用Rails 3.2.8和jQuery-Rails 2.1.3(jQuery 1.8.2正在应用程序中加载)。 然而,这个来自要点的coffeescript引起了第7行(评论后面)标题中的错误。 $ -> $modal = $(‘#modal’) $modal_close = $modal.find(‘.close’) $modal_container = $(‘#modal-container’) # Handle modal links with the data-remote attribute $.on ‘ajax:success’, ‘a[data-remote]’, (xhr, data, status) -> $modal .html(data) .prepend($modal_close) .css(‘top’, $(window).scrollTop() + 40) .show() $modal_container.show(); $.on ‘click’, ‘#modal .close’, -> $modal_container.hide() $modal.hide() false 关于这个问题的评论让我知道这个jQuery版本问题可能是原因,这就是为什么我检查了我的jQuery版本,但显然这个jQuery并不像解决这个问题那样古老。 我还validation了在此脚本之前加载了jQuery(或者至少,它在加载顺序中位于此脚本之上)。 […]

从rails 3.1中的html调用javascript函数

在rails 3.1中使用coffeescript,jQuery和sprockets将coffeescript文件编译为以下块: (function() { var a; var b; var c; foo = function() { alert(“foo”); } bar = function() { alert(“bar”); } }).call(this); 这似乎将函数foo和bar移出全局范围,因此bar可以被foo调用,但是不能从html代码调用。 当我尝试从select onchange元素调用foo时,我得到一个“找不到变量:foo”。 现在的解决方法是将所有全局可用的函数移动到.js文件。 但是这样做的正确方法是什么? 谢谢

Coffeescript – ‘this’总是被胖箭头回调中的’_this’取代

我想知道有可能以某种方式阻止this关键字被转换成_this内部胖箭头回调( => )? 例如: class someClass someMethod: -> $(document).on ‘click’, ‘.myclass’, (e) => # doing things with right context this, it’s ok @anotherMethod() @oneMoreMethod() # but here I need jQuery “this“ pointing to element $el = $ this # this is transformed into “_this“ 🙁 也许我错过了一些选项或运营商? 更新我知道像self = this的伎俩,但我认为CS有更优雅的东西..

在coffeescript / javascript 中使用正则表达式中的字符串中的“+”加号

我有一个正则表达式我正在运行以过滤表中的行。 过滤在Javascript中完成。 我正在写coffeescript,但Javascript解决方案没问题 – 我可以将其自己翻译成coffeescript。 我有一个值role ,包含我想要使用正则表达式过滤的字符串。 问题是字符串role 可能包含也可能没有嵌入“+”符号。 加号是正则表达式搜索的特殊字符,需要在搜索字符串中进行转义。 我像这样创建正则表达式搜索字符串(coffeescript): “^”+role+”$” 我如何预处理role以逃避任何“+”符号,以便正则表达式工作?

使用Sinon.js并阻止对我的应用服务器的调用

问题很简单: 我希望我们sinon.js测试一段javascript以确保它在做两件事时调用$.ajax方法: 我不想真正打到服务器 我想模拟服务器的响应 所以这是JS: $.ajax url: “/tickets/id.json” dataType: ‘json’ .done (data) => HandlebarsTemplates[“tickets/popup_title”] data 这是我的测试: describe ‘PopupDisplayer’, -> beforeEach -> loadFixtures ‘popup_displayer’ new PopupDisplayer @title_stub = sinon.stub( HandlebarsTemplates, “tickets/popup_title”) @jquery_stub = sinon.stub(jQuery, ‘ajax’).yieldsTo(‘done’, {}) //This triggers the ajax call $(‘.popupable .title’).mouseenter() afterEach -> HandlebarsTemplates[‘tickets/popup_title’].restore() HandlebarsTemplates[‘tickets/popup_content’].restore() jQuery.ajax.restore() @server.restore() it ‘renders the title with the data […]

如何使用jquery以railsforms显示/隐藏?

我有一个rails视图(erb),显示一些复选框(来自seed.rb的填充数据)..当选择了某些复选框时,我需要在它下面显示一个div来收集更多信息。例如:用户检查“周年纪念日”并且它下方的div显示要求日期。 jquery是最好的方法吗? Coffescript? *注意:我使用Wicked gem创建一个多步骤 inheritance人的观点: 下面是渲染的html(无论如何都是表单部分): New Years Day Valentine Day Easter Mother Day Father’s Day Halloween Thanksgiving Christmas 谢谢!

jquery中.done的角度等价物

我无法找到我想要做的替代解决方案,让我说我在jquery中有这个代码: $.get ‘file.json’, (re) -> for k, v in re tpl = “{{v.content}}”; $ ‘#container’.append tpl .done () -> impress().init() 这样工作正常,因为.done只在ajax之后执行代码,但angular似乎没有像.done ,并且impress().init()在内容加载时无法重新初始化,因此会出现错误关于数据绑定.. 这是我对角度的尝试 App.controller ‘SomeCtrl’, ($scope, $http) -> $http.get(‘file.json’) .success (res) -> $scope.slides = res #what could possibly be in here

jQuery点击事件两次触发?

这是一个相当奇怪的情况,你可以在这里看到代码: http://jsfiddle.net/zpZtH/2/

JQuery事件在生产中不适用于heroku,但在开发中工作

这似乎是一个常见的问题,但我没有找到适用于我的案例的解决方案。 我在bikes.js.coffee中有一些JQuery,可以在本地开发中正常工作。 当我推送到Heroku时,bikes.js.coffee中的脚本无法运行。 浏览器的javascript控制台中没有错误。 我正在使用Rails 4.0。 从阅读中我相信这是资产编制方式的一些错误,但我无法超越它。 所有图像在制作中都很好。 bikes.js.coffee: ready = -> jQuery ($) -> # when the #make field changes $(“#bike_make_id”).change -> # make a POST call and replace the content make = $(“select#bike_make_id :selected”).val() make = “0” if make is “” jQuery.get “/bikes/update_model_select/” + make, (data) -> $(“#bikeModels”).html data false $(document).ready(ready) $(document).on(‘page:load’, ready) […]