ruby on rails - Gain the javascript context within the *.js.erb response -


i not having javascript code in global scope. moduralize it. when use rails built-in ajax helpers, so:

<%= select_tag "task", options_for_select([ ["today", "0"], ["yesterday", "1"], ["next 7 days", "2"] ]), remote: true, 'data-params' => "task_category_id_is=#{category.id}&sort=#{params[:sort]||'date_due'}&embedded=true",  'data-url' => tasks_path, 'data-type' => 'html' %> 

i can hook using rails jquery-ujs functionaility e.g. 'ajax:success'.

my_app.utilities.card('.card-holder').init();  my_app.utilities.card = function (card_holder) {     var $card_holder = $(card_holder);     return {         card_filter: function(){             $card_holder.on('ajax:success', '.card-filter', function(event, data, status, xhr) {             $('.loading').hide();              var $current_card_holder = $(this).closest('.card-holder');             $current_card_holder.empty();             $current_card_holder.append(data);         })         }         init: function(){           this.card_filter();         }     } } 

this allows me scope ajax behavior within modules. however, notice had specify data-type html in select_tag helper. not want specify data-type html or json. want execute javascript instead because of complexity of the ajax. send :js data type, , respond in rails create.js.erb file perform complicated javascript:

# create.js.erb <% broadcast "/tasks/create" %>   if( $('#contact-tasks').find('tbody') )      $('#contact-tasks').find('tbody').find('td').filter(function() {         return $(this).text().trim() == "no data";     }).closest("tr").remove();    $('#contact-tasks').find('tbody').append("<%= escape_javascript render('tasks/task', task: @task, model: @taskable) %>") <% end %> 

the problem above in *.js.erb file lost javascript scope had. had reference $('#contact-tasks') in global scope. had no access closure $card_holder.

is there way respond :js content types *.js.erb file , still have access scope in ajax call made?


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo