I am going through Force.com Canvas Developer's Guide and I see the function Sfdc.canvas() used many places but it is not mentioned what it does exactly..Can someone give your input? For example, I see the code and not sure what the Sfdc.canvas() is doing.Thanks.

Sfdc.canvas(function() {        
    var login = Sfdc.canvas.byId("login"),
    loggedIn = Sfdc.canvas.oauth.loggedin(),
    token = Sfdc.canvas.oauth.token()
    login.innerHTML = (loggedIn) ? "Logout" : "Login";
    if (loggedIn) {
    // Only displaying part of the OAuth token for better formatting.
    Sfdc.canvas.byId("oauth").innerHTML = Sfdc.canvas.oauth.token()
    .substring(1,40) + "…";

The function passed to Sfdc.canvas runs when the dom is ready. In this case it:

  • Grabs the element with the id "login"
  • Gets the current oauth state
  • Grabs the current token
  • Sets the "login" element to either say "Login" or "Logout"
  • If it is logged in, it prints the first 40 char of the oauth token
  • It attaches the login handler to the "login" element's onClick

Long story short, it is a convenience method for onready.

Source: A comment in this example, near line 31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.