2015-11-25 6 views
7

मैं chating मॉड्यूल पर काम कर रहा हूं। इसके लिए मैंने private_pub gem का उपयोग किया है। इस मॉड्यूल में मैंने तीन चैनल बनाए हैं, लेकिन मैं उन सभी का जिक्र नहीं कर सकता, क्योंकि यह एक बहुत, बहुत बड़ा पेज दिखाएगा। तो चलिए एक चैनल से चिपके रहें।js.erb फ़ाइल दो बार निष्पादित कर रही है। रेल पर रूबी - private_pub

मैं चैनल होने "<%= subscribe_to "/conversations/send_invitation" %>" और फिर ajax काम करता है एक लिंक पर क्लिक करें और "conversations/send_invitation" को जाता है (केवल एक बार - जो ठीक है) और मेरे "/conversations/send_invitation" मैं

def send_invitation 
@conversation = Conversation.new(conversation_params) 
respond_to do |format| 
format.js { render :layout => false } 
format.html 
end 
end 

और फिर मेरी send_invitation.js.erb फ़ाइल मैंने में है निम्नलिखित

<% publish_to "/conversations/send_invitation" do %> 
var recipient_id = "<%[email protected] %>"; 
var current_temp_user = $("#current_temp_user").val(); 

if(recipient_id == current_temp_user){ 
console.log('here') 
$("#invitation_div").html("<%=j(render :partial => '/restaurants/invitation')%>"); 
    card_modal = $('#invitation_card').modal(); 
    card_modal.modal('show'); 
} 
<% end %> 

यहाँ मेरी application.js फ़ाइल है

//= require jquery 
// require jquery-ui 
//= require jquery_ujs 
// require turbolinks 
//= require bootstrap 
//= require jquery.raty 
//= require private_pub 
//= require_tree . 

और recipient side (जो अच्छा है) पर मोडल डिस्प्ले।

अब समस्या यह है कि मैं अपने firefox console/html में same id के साथ दो modal देख सकता हूं। closing/submitmodal के लिए buttons भी काम करना बंद कर दिया। console.log में फ़ाइल में console.log परिणाम दो बार दिखाया गया है, इसलिए यह स्पष्ट है कि js.erb फ़ाइल दो बार निष्पादित हो रही है। लेकिन ध्यान देने योग्य बात यह है कि ajax केवल एक बार भाग गया। मैं इस पर घंटे की जोड़ी बर्बाद किया है और केवल This link पाया जो भी, मेरे लिए काम नहीं किया क्योंकि जब मैं या तो jquery.js या jquery_ujs.js यह मुझे देता है त्रुटियों

+0

क्या आपके सर्वर लॉग कहता है? क्या क्रिया 'send_invitation' एक या दो बार निष्पादित की गई है? हो सकता है कि आप दो बार चैनल की सदस्यता ले रहे हों? आप ब्राउज़र के डेवलपर कंसोल में इसे डीबग कर सकते हैं। – coorasse

+0

@coorasse नहीं, यह 'send_invitation' को केवल एक बार – ImranNaqvi

+0

पर कॉल कर रहा है, तो क्या आप डेवलपर कंसोल में देख सकते हैं यदि आपने चैनल में दो बार सदस्यता ली है? – coorasse

उत्तर

0

मैं एक समाधान नहीं मिला सकता है निकालने के लिए, लेकिन मैं यह सोचा वास्तविक मुद्दे के बारे में सूचित करने के लिए किसी के लिए मूल्यवान होना चाहिए।

मेरे मामले में मुद्दा यह था कि मैं एक ही पृष्ठ पर दो चैनल बना रहा था, यही कारण है कि यह पतली सर्वर के साथ दो बार हैंडशेक बना रहा था।

नोट लेकिन एक सामान्य मामले में यह भी हो सकता है यदि हमने जेएस फ़ाइल को दो बार शामिल किया हो।

+0

उसी पृष्ठ पर चैनल बनाने का क्या अर्थ है? <% = Subscribe_to conversation_path (वार्तालाप)%> पृष्ठ पर दो बार और एक वार्तालाप फ़ॉर्म या <% = subscribe_to conversation_path (वार्तालाप)%> केवल एक बार होने पर, लेकिन दो वार्तालाप फ़ॉर्म होने के बाद? मेरे मामले में यह निश्चित रूप से एक टर्बोलिंक मुद्दा है। अगर मैं एक पूर्ण पृष्ठ पुनः लोड करता हूं तो सब कुछ ठीक काम करता है, लेकिन अगर मैं ऐप के पेज पर किसी अन्य पृष्ठ पर जाने के बाद वार्तालाप बना देता हूं और फिर वापस आ रहा हूं तो यह अधिक बार प्रदर्शित होता है (यह डीबी को केवल 1 संदेश बचाता है, केवल समस्या है प्रदर्शित करने के साथ)। कोई अंदाज़ा? –

+0

इमरान, ऐसा लगता है कि मैं इस मुद्दे को हल कर सकता हूं। मेरा जवाब देखें! –

0

$(document).on('keydown', '.chatboxtextarea', function (event){....}); के साथ मेरा संदेश ऑब्जेक्ट केवल एक बार बनाया गया था, लेकिन कई बार प्रदर्शित हुआ कि मैं पेज पर कितनी बार पूर्ण पृष्ठ रीलोड के बिना टर्बोलिंक के साथ लौट आया। js.erb फ़ाइल में इसके आसपास काम करने के लिए मैंने event जोड़ा। यह जांचने के लिए कि आंशिक पहले से जोड़ा गया है या नहीं।

application.js

//= require jquery 
//= require jquery-ui 
//= require jquery_ujs 
//= require turbolinks 
//= require jquery.remotipart 
//= require chat 
//= require refile 
//= require bootstrap-sprockets 
//= require private_pub 
//= require local_time 
//= require moment 
//= require fullcalendar 
//= require bootstrap-datetimepicker 
//= require_tree . 

JS फ़ाइल

//submitting chat message (it's OUTSIDE $(document).on('page:change'..) 
//bind submit to document --> not affected by turbolinks 
$(document).on('keydown', '.chatboxtextarea', function (event) { 
    var id = $(this).data('cid'); 
    checkInputKey(event, $(this), id); 
}); 

function checkInputKey(event, chatboxtextarea, conversation_id) { 
    if (event.keyCode == 13 && event.shiftKey == 0) { 
    event.preventDefault(); 

    //checking if field is empty and submitting the form 
    message = chatboxtextarea.val(); 
    message = message.replace(/^\s+|\s+$/g, ""); 
    if (message != '') { 
     $('#conversation_form_' + conversation_id).submit(); 
    } 
    } 
} 

html.erb रूप

<div class="chatboxinput"> 
    <%= form_for([@conversation, @message], :remote => true, :html => {id: "conversation_form_#{@conversation.id}"}) do |f| %> 
    <%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %> 
    <% end %> 
</div> 
...... 
<%= subscribe_to conversation_path(@conversation) %> 

_message.html.erb

<li class="<%= self_or_other(message) %>"> 
    <div class="chatboxmessagecontent"> 
    <p> 
     <%= message.body %> 
    </p> 
    </div> 
</li> 

create.js।ERB

<% publish_to @path do %> 
    var id = "<%= @conversation.id %>"; 
    var chatbox = $(".chatboxcontent"); 
    var lastMessage = chatbox.children().last(); 

    //with this one message will be displayed only once 
    if(event.handled !== true) { 

    $message = $('<%= j render @message %>'); 
    chatbox.append($message); 
    chatbox.scrollTop(chatbox[0].scrollHeight); 

    //for displaying incoming and outgoing messages differently 
    if(sender_id != receiver_id) { 
     chatbox.children().last().removeClass("self").addClass("other"); 
     chatbox.scrollTop(chatbox[0].scrollHeight); 
    } 

    event.handled = true; 
    }; 
<% end %> 
0

उपयोग अनबाइंड विधि अपने घटनाओं के साथ निष्पादित करने के लिए अवांछित/एकाधिक कॉल दूर करने के लिए। यह चाल करेगा। लेकिन एकाधिक कॉल के पीछे कारण अभी तक स्पष्ट नहीं है।

$("#foo").unbind("click", handler); 

उम्मीद है कि यह मदद करता है। इस मणि

0

उपयोग बजाय

gem "private_pub", :git => 'https://github.com/ryleto/private_pub.git'

संबंधित मुद्दे