Can't get snippet to run on page load

j_flatterj_flatter Member
edited May 2019 in Problems
Mostly because I don't know how :)

I have a script which works fine when pasted into the console, but I would like for it to run on Page Load.

var params = arguments[0],

    step = params.step,

$ = skuid.$;

var userModel = skuid.model.getModel('User'),
    user = userModel.getFirstRow(),
   weekUpdateModel = skuid.model.getModel('WeeklyUpdate'),
   weekUpdate = weekUpdateModel.getFirstRow();
var userAlias = user.Alias + ' Update - ' + user.Today__c;
weekUpdateModel.updateRow(weekUpdate,'Note',userAlias);
$.each(weekUpdateModel.registeredItems,function(){
  this.refreshFields();
});

Thanks,

Jacob

Comments

  • Rob HatchRob Hatch Skuad, Sonar ✭✭
    edited September 2016
    You actually want this snippet to run AFTER page load,  because you need the model data to be available.  This is why it runs successfully from the console.  Models have loaded etc.  You need to wrap your snippet in a function that waits for the page to load before executing.  Somthing like this: 
    (function(skuid){    var $ = skuid.$;    $(function(){       Your Function goes here......    }); })(skuid);
    Actually if you create a new Javascript resource of type Inline you will get this code prepopulated for you.  
  • j_flatterj_flatter Member
    edited April 2014
    Thanks Rob! Learning JS by sheer force is challenging sometimes.
  • Gary BaileyGary Bailey Member
    edited November 2017
    Page loading snippet
  • Sharon AnoopSharon Anoop Member
    edited August 2015
    Hi Rob, This works fine for a page. But when i use this page as a popup in another, its not working. Can u please suggest a solution for it? Thanks!
  • Irvin WaldmanIrvin Waldman Member
    edited May 2019
    (function(skuid) {    
        var $ = skuid.$;
        // Register a snippet to run
        skuid.snippet.registerSnippet('HelloWorld', function() {
            console.log("Go Skuid!");
        });
        // Called on page load
        function init() {
            skuid.snippet.getSnippet('HelloWorld')();
        }
        // Run the snippet initially on page load
        $('.nx-page').one('pageload', function() {
            init();
        });
    })(skuid);
  • Sharon AnoopSharon Anoop Member
    edited August 2015
    Thanks! Found the solution :)
Sign In or Register to comment.