Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current year with coffeescript

I am working with Angular/CoffeeScript/Rails and I am trying to place the copyright with the current year in the footer.

With JavaScript, I used to do something like this:

<script type="text/javascript">
<!--
    var theDate = new Date();
    $('.mDate').html('&copy; ' + theDate.getFullYear());
-->
</script>
<span class="mDate"></span>

Here is what I have tried in CoffeeScript - within the mainController.coffee:

  # mCopyRight
  # ------------------------------------------------------------
  $scope.mCopyRight = ->
    $scope.theDate = new Date();
    $scope.mFullYear = '&copy; ' + theDate.getFullYear();

Within the application.html.erb

  <span class="mDate">{{mFullYear}}</span>

But nothing is showing up.

It is like new Date is not working and/or .getFullYear(); either

The only thing that I can think of, would be to make a regular js file and require it in the application.js file:

//= require user-defined/mDate

With a file named mDate.js being in the vendor/assets/javascripts/user-defined/ directory.

Thanks in advance

like image 983
kronus Avatar asked Aug 04 '26 10:08

kronus


1 Answers

You have assigned theDate to be a property of $scope. Either read it from there:

  $scope.mCopyRight = ->
    $scope.theDate = new Date
    $scope.mFullYear = '&copy; ' + $scope.theDate.getFullYear()

Or if that value is only needed by this function, don't use $scope:

  $scope.mCopyRight = ->
    theDate = new Date
    $scope.mFullYear = '&copy; ' + theDate.getFullYear()
like image 200
Tim B Avatar answered Aug 06 '26 22:08

Tim B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!