0

I implemented a simple search while I feed to my database, so I can see if the record with the name already exists:

function doneTyping() {
  $.ajax({
    url: $('#existance_route').val(),
    type: "GET",
    dataType: "json",
    data: { place_name: $name_input.val() },
    complete: function() {},
    success: function(data, textStatus, xhr) {
      setupTextfieldsAddonsWith(data);
      console.log("Response received!");
    },
    error: function() {
      console.log("AJAX error!");
    }
  });
};

setupTextfieldsAddonsWith(data) just simply tell the user (by the UI) that a records exists or not.

However I have an issue. Sometimes when I go to the link to create a new record, it just doesn't work. But then I reload the page manually and it starts working.

Does anyone have any idea why I receive such behavior?

PS. If there are "wiser" plugins for doing the thing that I describe please let me know. I only found Client Side Validations but it is now exactly what I need to do

EDIT

This is how I trigger doneTyping (code from one of the SO questions):

var typingTimer;
var doneTypingInterval = 500;
// place_name is an id bind to a text_field
$('#place_name').on('keyup', function() {
  clearTimeout(typingTimer);
  typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

$('#restaurant_name').on('keydown', function () {
  clearTimeout(typingTimer);
});
Kyle Macey
  • 8,074
  • 2
  • 38
  • 78
mdmb
  • 4,833
  • 7
  • 42
  • 90
  • How are you triggering doneTyping function? – Sahil Sep 09 '16 at 18:55
  • I updated the question for you @sahil – mdmb Sep 09 '16 at 19:16
  • So you are saying that on every first load of the page, the function doesn't fire and you have to refresh the page to do so? Are you placing the functions inside `$(function(){ // your code});`, this will make sure that jQuery has loaded. – Sahil Sep 09 '16 at 19:28
  • It is placed inside tis function. And yes, sometimes the function doesn't fire, and it seems that it does not after I use the navigation bar in my rails app to go to the page (the link is changed etc.). – mdmb Sep 09 '16 at 19:37
  • Do you see any requests being made in browser console/Rails console when you start typing in scenarios where you say nothing happens? – Sahil Sep 09 '16 at 19:43
  • Not at all - as indicated by code, I should receive "Response received" every time the timer goes to 0 – mdmb Sep 09 '16 at 19:59
  • Can you add a `console.log` statement just at the beginning of `doneTyping`, and also if the search box field is getting updated by your ajax function then change your keydown, keyup functions like this: `$(document).on('keyup', '#place_name',function() `, `$(document).on('keydown', '#restaurant_name',function ()`. – Sahil Sep 09 '16 at 20:05
  • It seems like the function $(function() { }) is not triggered when I switch to the page using navbar. I added a console.log statement at the end of this function, but it is only triggered when I reload the page manualy, and not jump to it using navbar – mdmb Sep 09 '16 at 22:16

1 Answers1

1

Ok, so I did some more research and found this: click

Basically adding my functions inside

$(document).on('turbolinks:load', function() {

  ...your javascript goes here...

});

makes everything work as expected!

Community
  • 1
  • 1
mdmb
  • 4,833
  • 7
  • 42
  • 90