coffeescript

Handlebars Block Helper : each with sort

☆樱花仙子☆ 提交于 2020-03-01 02:04:44
问题 I have an array of json objects which I output using a Handlebars template; I am currently doing using {{#each object}}...{{/each}}. I now need to sort the objects by one of the object's properties, which again is no problem using a handlebars helper & coffeescript, however, I have a problem in my template in that I cannot work out how to iterate over the sorted array using each. My research so far indicates that I probably need to write a custom Handlebars helper which will, in effect, be: {

jQuery / JavaScript替换损坏的图像

孤街浪徒 提交于 2020-02-26 14:02:04
我有一个包含一堆图像的网页。 有时,该图像不可用,因此在客户端的浏览器中会显示损坏的图像。 如何使用jQuery获取图像集,将其过滤为损坏的图像然后替换src? -我认为使用jQuery会更容易,但是事实证明,仅使用纯JavaScript解决方案(即Prestaul提供的解决方案)要容易得多。 #1楼 如果像我这样的人尝试将 error 事件附加到动态HTML img 标签上,我想指出的是,有一个陷阱: 显然,与大多数 标准 相反, img 错误事件在大多数浏览器中 不会冒泡 。 因此,类似以下内容将 不起作用 : $(document).on('error', 'img', function () { ... }) 希望这对其他人有帮助。 我希望我已经在此线程中看到了这一点。 但是,我没有。 所以,我添加了它 #2楼 我使用内置的 error 处理程序: $("img").error(function () { $(this).unbind("error").attr("src", "broken.gif"); }); 编辑: 在 jQuery 1.8 及更高版本中不建议使用 error() 方法。 相反,您应该使用 .on("error") 代替: $("img").on("error", function () { $(this).attr("src", "broken

如何使用jQuery选择文本节点?

淺唱寂寞╮ 提交于 2020-02-26 04:53:14
我想获取一个元素的所有后代文本节点,作为jQuery集合。 最好的方法是什么? #1楼 也可以这样完成: var textContents = $(document.getElementById("ElementId").childNodes).filter(function(){ return this.nodeType == 3; }); 上面的代码从给定元素的直接子级子节点中过滤textNodes。 #2楼 对我来说,普通的老式 .contents() 似乎可以返回文本节点,只需要小心选择器,以便您知道它们将是文本节点。 例如,这用 pre 标签将TD的所有文本内容包装在了我的表中,没有问题。 jQuery("#resultTable td").content().wrap("<pre/>") #3楼 jQuery.contents() 可与 jQuery.filter 一起使用以查找所有子文本节点。 稍作改动,您就可以找到孙子文本节点。 无需递归: $(function() { var $textNodes = $("#test, #test *").contents().filter(function() { return this.nodeType === Node.TEXT_NODE; }); /* * for testing */ $textNodes.each

Coffeescript memoization?

妖精的绣舞 提交于 2020-02-25 10:11:12
问题 I have a function that displays a number as a properly formatted price (in USD). var showPrice = (function() { var commaRe = /([^,$])(\d{3})\b/; return function(price) { var formatted = (price < 0 ? "-" : "") + "$" + Math.abs(Number(price)).toFixed(2); while (commaRe.test(formatted)) { formatted = formatted.replace(commaRe, "$1,$2"); } return formatted; } })(); From what I've been told, repeatedly used regexes should be stored in a variable so they are compiled only once. Assuming that's

A puzzle about this/@ in Javascript/Coffeescript

孤街醉人 提交于 2020-02-02 02:55:32
问题 I'm working through Trevor Burnham's CoffeeScript book and I've run into a weird puzzle concerning this / @ . The puzzle has a few parts (and I may be just very confused), so I'll try to make this as clear as I can. The main problem I'm having is that I get varied and inconsistent results running the same code through different REPLs and interpreters. I'm testing with (1) the coffee REPL and interpreter, (2) Node's REPL and interpreter and (3) v8's REPL and interpreter. Here's the code, first

Default function parameter ordering

老子叫甜甜 提交于 2020-01-30 13:57:09
问题 Reading through this, I came to the bit on default values for function arguments: fill = (container, liquid = "coffee") -> "Filling the #{container} with #{liquid}..." That's neat, but then I tried this: fill = (container="mug", liquid = "coffee") -> "Filling the #{container} with #{liquid}..." alert fill(liquid="juice") and got the unexpected alert with "Filling the juice with coffee..." . So then I tried this: fill = (container="mug", liquid = "coffee") -> "Filling the #{container} with #

How to delegate `hover()` function by using `on()` [duplicate]

两盒软妹~` 提交于 2020-01-30 07:08:44
问题 This question already has answers here : JQuery .on() method with multiple event handlers to one selector (6 answers) Closed 5 years ago . I had a coffee code like this. $('.foo').hover \ (-> $(this).css "cursor", "pointer"), \ (-> $(this).css "cursor", "default") And I want to apply this function to dynamically appended DOM, so I tried to delegate function by using on() like this. As a example I referred this question $(document).on 'hover', '.foo', (event) -> $(this).css "cursor", "pointer"

CoffeeScript: Getter/Setter in Object Initializers

心不动则不痛 提交于 2020-01-28 13:04:37
问题 ECMAScript allows us to define getters or setters as following: [text/javascript] var object = { property: 7, get getable() { return this.property + 1; }, set setable(x) { this.property = x / 2; } }; I can work around if I'm using a class : [text/coffeescript] "use strict" Function::trigger = (prop, getter, setter) -> Object.defineProperty @::, get: getter set: setter class Class property: '' @trigger 'getable', -> 'x' member: 0 But what if I want to define trigger on the object directly -

Gem 'coffee-rails' Load Error: HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory

╄→гoц情女王★ 提交于 2020-01-25 08:36:24
问题 I try to deploy Ruby on Rails with nginx and passenger.But I get following error in /var/log/nginx/error.log [ E 2018-09-19 12:07:12.5312 24363/Tb age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /home/example/test_app: The application encountered the following error: There was an error while trying to load the gem 'coffee-rails'. Gem Load Error is: HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory Backtrace for gem load

jquery events not firing using webpacker and coffeescript

放肆的年华 提交于 2020-01-24 18:06:30
问题 Trying to build a new rails 6 application from scratch to replace a rails 5.2 application. Trying to use webpacker with foundation-site jquery and coffeescript After a bunch of searching I've got everything loaded with webpacker. It appears to be very (maybe too) flexible in that examples use different approaches. Everything works (foundation does x-grid, callouts etc, jquery loads and I can log jquery objects, coffeescript compiles) except jquery events don't seem to fire or build a listener