Sinatra: rendering snippets (partials)

Posted by Michael on Stack Overflow See other posts from Stack Overflow or by Michael
Published on 2012-09-06T03:35:47Z Indexed on 2012/09/06 3:38 UTC
Read the original article Hit count: 136

Filed under:
|

I'm following along with an O'Reilly book that's building a twitter clone with Sinatra. As Sinatra doesn't have 'partials' (in the way that Rails does), the author creates his own 'snippets' that work like partials. I understand that this is fairly common in Sinatra.

Anyways, inside one of his snippets (see the first one below) he calls another snippet text_limiter_js (which is copied below). Text_limiter_js is basically a javascript function.

If you look at the javascript function in text_limiter_js, you'll notice that it takes two parameters. I don't understand where these parameters are coming from because they're not getting passed in when text_limiter_js is rendered inside the other snippet.

I'm not sure if I've given enough information/code for someone to help me understand this, but if you can, please explain.

=snippet :'/snippets/text_limiter_js'
%h2.comic What are you doing?
%form{:method => 'post', :action => '/update'}
  %textarea.update.span-15#update{:name => 'status', :rows => 2, :onKeyDown => "text_limiter($('#update'), $('#counter'))"}
  .span-6
    %span#counter
      140
    characters left
  .prepend-12
    %input#button{:type => 'submit', :value => 'update'}

text_limiter_js.haml

:javascript
  function text_limiter(field,counter_field) {
    limit = 139;
    if (field.val().length > limit) 
      field.val(field.val().substring(0, limit));
    else
      counter_field.text(limit - field.val().length);
  }

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sinatra