1

I'm very beginner in javascript and right now im learning from railscast. I have problem how to write this script, writen in coffee script to normal JS. Can somebody do this:

jQuery ->
  if $('.pagination').length
    $(window).scroll ->
      url = $('.pagination .next_page').attr('href')
      if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
        $('.pagination').text("Fetching more products...")
        $.getScript(url)
   $(window).scroll()

  $('.tooltip').tooltipster(
    animation: 'grow'

    );

I try with " jQuery(function($) { " but it doesnt work.

2
  • 1
    If you show your entire attempt we could point out where you went wrong. Commented Jul 14, 2013 at 12:24
  • 1
    compile it?! coffee -c file.coffee Commented Jul 14, 2013 at 18:49

3 Answers 3

2

I use http://js2coffee.org/ to convert between the two

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, seems very nice
0

You'd have to do it this way :

jQuery(function() {
  if ($('.pagination').length) {
    $(window).scroll(function() {
      var url;
      url = $('.pagination .next_page').attr('href');
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
        $('.pagination').text("Fetching more products...");
        return $.getScript(url);
      }
    });
  }
  return $(window).scroll();
});

$('.tooltip').tooltipster({
  animation: 'grow'
});

Wherever you see ->, change it to function() :)

10 Comments

ok thanks but the tooltip right now doesnt work - endless pagiantion also doesnt work. what more i should change?
make fiddle? i dont undestand
leave that.. do you see any console errors in chrome? Press f12 after your page is loaded and check the bottom right hand corner of the window which will open and tell us..do u see any red x marks there?
ok in here works pagination but doesnt work tooltip. I remove comments form top of this file and it starts working
no there is no errors - scroll works correctly but tooltip does not. HI think that there is no return for this tooltipster - but when i ad it, it gives error
|
0
$(document).ready(function(){
  if ($('.pagination').length > 0)
    $(window).scroll(function(){
      var url = $('.pagination .next_page').attr('href')
      if (url != null && $(window).scrollTop() > $(document).height() - $(window).height() - 50){
          $('.pagination').text("Fetching more products...");
          $.getScript(url);
        }
    });
    $(window).scroll();

  $('.tooltip').tooltipster(
    animation: 'grow'
  );
});

Changes:

  1. --> => function(){}
  2. if url => if(url != null...)
  3. some semicolons ;
  4. if length => if(length > 0)

As a side note: Include your script after including jQuery.

I saw you in a comment including a file in the js. That's not the way to include jQuery scripts or even pure js scripts in HTML. You need to do it like this(add this in the <head> of you HTML markup:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="your_file_name.js" type="text/javscript"></script>

5 Comments

@efectiva see my edit and tell that if that works! and make sure to include jQuery before your script.
ok it doesnt work. pagination and tooltipster doesnt work. I change file name to users.js and in application.js add - //= reguire users.js - and it doesnt work.
@efectiva NO! I believe you cannot do that! You need to include you jQuery like this <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javscript"></script> in your HTML markup.
in ruby in rails ? i never see that type of include .. all my js script are automaticaly included in rails --- i think ... i have 2 examples of working site and there is no that type of includes - every thing is in application.js
when i check in chrome i see that this script is in source users.js , jquery and css files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.