I am trying to employ auto save to a page so that any field that is edited is automatically saved to the model. This is pretty easy through model actions, but the problem is that the save action is too quick. This is a particular issue for date fields if you are manually typing them in. Is there a way to delay this action. In the future, it might be a nice feature to have a delay setting on the Event Trigger so that any event trigger could be delayed a number of seconds before the action is executed. Any help on this would be appreciated. Thanks
You could introduce a delay to your Action sequence by adding a Run Snippet action at the beginning that delays the execution of actions in the sequence by a fixed amount of time. Here is what such a Snippet would look like: here Iâm waiting 5 seconds but you can change that easily:
var SECONDS_TO_DELAY = 5;
var dfd = skuid.$.Deferred();
setTimeout(function(){
dfd.resolve();
},SECONDS_TO_DELAY*1000);
return dfd.promise();
Many thanks! Works great!
Hi Zach⌠Follow up. Is there a way to set a delay based on seconds after last character typed? If someone is in a text field and typing a 15 word sentence, I would want the auto save to occur when they have stopped typing for 1 or 2 seconds. Is that possible?
Yes this is possible - just a slight adjustment to the JavaScript so that we keep track of a timer which we will then reset every time the user types another character. In your Skuid Page, you need to change the Resource Location of the JavaScript Resource from Inline (Snippet) to Inline - very important! - and then replace the Resource Body with this:
(function(skuid){
var $ = skuid.$;
var timer;
skuid.snippet.registerSnippet(âWait5Secondsâ,function(){
  var SECONDS_TO_DELAY = 2;
    var dfd = skuid.$.Deferred();
    // Clear existing timer
    clearTimeout(timer);
    timer = setTimeout(function(){
      dfd.resolve();
    },SECONDS_TO_DELAY*1000);
    return dfd.promise();Â
});
})(skuid);
NOTE: I changed SECONDS_TO_DELAY in the snippet above to be 2 - but I left the Snippet name at âWait5Secondsâ. You can change the Snippet Name, just make sure you change the Run Snippet action in your Model Action Sequence as well.
By the way, I think 2 seconds is too fast â itâs not the best user experience. Iâd back it up to at least 5, if not more. In my testing of this I found 2 seconds to be rather annoying, it doesnât even give you enough time to switch the field youâre editing.
You are now in a tie with Rob for worldâs best Skuidery. Thanks!. The first code works perfectly, the second doesnât seem activate any delay. I made sure to switch to inline. I donât get any error messages, but the save occurs instantly.
Are you sure that in your Model Actions your âRun Skuid JavaScript Snippetâ action is set to âRun Snippet: Wait5Secondsâ?
SoooâŚIâve tried both of these, and even tried only having the trigger happen on non-narrative fields, but itâs still âglitchy.â When users are typing in a text box, the form auto-saves and throws them out of where they are (sometimes removing the last few characters that theyâve typed). Ideas? Iâm pretty sure adding this to my model is pretty idiot-proof but itâs always entirely possible Iâm doing something wrongâŚ
In case someone else comes across this, I have come up with a better solution to auto saving text fields.
- create a duplicate model to the one you will be editing the text field.
- create the following model actions on your primary model
A) activate model and set value on your secondary model so that the Record Id equals {{Id}}
B) query secondary model (now it will only have one row that will be the same as the row you are working on in primary model
C) update rows in secondary model to equal the text field (fields) in your primary model.
D) save secondary model
This will keep primary model and secondary model in sync and automatically save changes in the secondary model every time a change is made. It does it all behind the scenes without locking the UI
For flair, I have a rich text component inside a wrapper that pops up every time there are unsaved changes in the secondary model that says âsavingâŚ.â. Once the secondary model completes its save, the rich text component disappears letting the user know it was saved.