简体   繁体   English

从 html 获取字符串到 coffeescript

[英]Get string from html to coffeescript

I cannot seem to be able to pass a string from html我似乎无法从 html 传递字符串

<div class="download-by-date" data-hello="dd/mm/yy">

to coffeescript:至 coffeescript:

$('.datepicker').datepicker({ dateFormat: $(this).data('hello') })

Here is the code from the slim file这是来自 slim 文件的代码

= render layout: 'shared/section', locals: { title: 'Export on Demand', description: "Use this section to export all orders within a particular date period or starting from a reference" } do
  .download-by-date data-hello='dd/mm/yy'
    .row
      .column.medium-6
        label

How can I read the data-hello attribute correctly in my coffeescript file?如何在我的 coffeescript 文件中正确读取 data-hello 属性? I'm trying to get this in coffeescript:我试图在 coffeescript 中得到这个:

$('.datepicker').datepicker({ dateFormat: "dd/mm/yy" })

You can use either of these:您可以使用以下任何一种:

/*
#coffee script:

$('.download-by-date').on 'click', ->
  $('.datepicker').datepicker { dateFormat: $(this).data 'hello' }
*/

//compiled javascript
$('.download-by-date').on('click', function() {
  return $('.datepicker').datepicker({
    dateFormat: $(this).data('hello')
  });
});

Or或者

/*
#coffee script:

$('.download-by-date').on 'click', => 
  format = $('.download-by-date').data "hello"; 
  $('.datepicker').datepicker { dateFormat: format }
*/

//compiled javascript
$('.download-by-date').on('click', () => {
  var format;
  format = $('.download-by-date').data("hello");
  return $('.datepicker').datepicker({
    dateFormat: format
  });
});

Notice the difference between use of -> (thin arrow) and => (fat arrow).注意使用-> (细箭头)和=> (粗箭头)之间的区别。 The fat arrow binds the callback function to value of this at the definition spot.粗箭头在定义点将回调 function 绑定到this的值。 You want to use thin arrow here.你想在这里使用细箭头。

For more info refer:欲了解更多信息,请参阅:
Bound (Fat Arrow) Functions绑定(粗箭头)函数
CoffeeScript, When to use fat arrow (=>) over arrow (->) and vice versa CoffeeScript,何时在箭头 (->) 上使用粗箭头 (=>),反之亦然

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM