The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Rails javascript generation and conditions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Maxim Kulkin

Posts: 58
Nickname: hapk
Registered: Sep, 2006

Maxim Kulkin is developer in Selectosa Systems.
Rails javascript generation and conditions Posted: Sep 13, 2006 7:02 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Maxim Kulkin.
Original Post: Rails javascript generation and conditions
Feed Title: Software development
Feed URL: http://maximkulkin.blogspot.com/feeds/posts/full?alt=rss
Feed Description: Software development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Maxim Kulkin
Latest Posts From Software development

Advertisement
I've recently faced the problem: I need to generate javascript that would insert some HTML block unless it is already there. So, the code should be something like this:

if( !($('block_id')) ) {
// insert block
}

All that should go to RJS template.

First revision:

page << "if( !($('block_id')) ) {"
page.insert_html :before, 'container_id', '
'
page << "}"

Next, it would be nice to make use of JavaScriptProxy objects (that are spawn, e.g., by page#[] method). The problem is that HTML is emited when those proxy created or evaluated, thus it's not possible to wrap that javascript code into e.g. "if( ... )"..
Solution, first try:

class << page
def if
self << "if("
end
def then
self << ") {"
end
def end
self << "}"
end
end

page.if
page['block_id']
page.then
page.insert_html .......
page.end

This is better then the first one, but too ugly though. A better solution would include some black magick:

module AcitiveView
module Helpers
class JavaScriptProxy
def respond_to?(name)
name.to_sym == :to_json
end

def to_json
@generator.instance_variable_get('@lines').pop.chomp(';')
end
end
end
end

Then:

class << page
def if(expr)
page << "if( #{expr.respond_to?(:to_json) ? expr.to_json : expr} ) {"
yield if block_given?
page << "}"
end
end

page.if page['block_id'] do
page.insert_html .....
end

And finally, the more peaceful variant:

module ApplicationHelper
def if_exists(element_id)
page << "if( !($('#{element_id}')) ) {"
yield if block_given?
page << "}"
end
end

page.if_exists 'block_id' do
page.insert_html .....
end

Read: Rails javascript generation and conditions

Topic: Ruby Stub Variations: OpenStruct Previous Topic   Next Topic Topic: Languages used in Ajax development: the most - Java, the least - Ruby

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use