2012-12-19 9 views
6

In ruby ​​on rails, sto tentando di aggiornare 2 partial.Posta risposta con render parziale che ha eventi non funziona

show.html.erb:

<div id="filters"><%= render :partial => "pricelistfilters" %></div> 

pricelistfilters.html.erb:

<% @properties.each do |prop| %> 
    #Render the page on properties (and the newproperties) 
    #<select ...><option>...</option><option>...</option> 
    #</select> 
<% end %> 

products.js -> eventi per il rendering parziale

$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').change(function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

products_controller.rb -> codice per elaborare le modifiche apportate

def show 
    #Changing the properties (and the pricelist properties) 
    @properties #is filled 
    @newproperties #is filled 
    respond_to do |format| 
    format.html 
    format.js 
    end 
end 

show.js.erb

$('#filters').html('<%= escape_javascript(render :partial => "pricelistfilters") %>'); 
selectionChanges(); 

Il mio articolo è reso perfettamente bene. Tuttavia, quando ho la risposta corretta con l'oggetto reso, Semplicemente non mando più l'ajax. Quindi l'evento sui miei articoli selezionati è andato, mentre sono certo che li ho ripristinati con "selectionchanges();" alla fine del mio file show.js.erb?

Qualcuno può vedere una soluzione a questo?

Saluti e grazie in anticipo

+0

I blocchi di codice si comportavano in modo strano: grazie a @jdl per avermi aiutato! – ReBa

+0

Prego. Spero che tu trovi una buona risposta per il tuo problema. – jdl

+3

hai 'selectionchanges' AND 'selectionChanges', è un refuso? – RadBrad

risposta

2

Provare a utilizzare la funzione diretta di jQuery su product.js, come muggito:

$(window).ready(function(){ 
    selectionchanges(); 
}); 
function selectionchanges(){ 
    $('#filters select').live('change', function(){ 
    //Doing stuff to send with ajax 

    //Ajax: 
    $.ajax({ 
     url: document.URL, 
     type: 'POST', 
     data: params 
    }); 
    }); 
} 

In sostanza, si sta sostituendo elemento del DOM HTML che prima vincolato il evento 'cambia' in. Con live, anche sostituendo l'elemento, jQuery eseguirà nuovamente il binding per te.

Spero che aiuti!

+0

Dovrebbe dare un'occhiata a questo. Funzionando perfettamente! Grazie compagno. – ReBa

Problemi correlati