Posts Tagged ‘ajax’

rajax.js

// RAjax Namespace
var RAjax = {};

// Basic Logger facility that supports logging to
// an HTMLElement, the Firebug console and alerts.
// If provided with a valid DOM id, it will append
// log messages to this element. If it can't find
// the DOM id you provided, it will try to use
// the Firebug console instead (you can make sure
// that this will be found by installing Firebug Lite
// from http://www.getfirebug.com/lite.html website).
// Finally, if it can't find a console Object to log to,
// and you tell it so by passing true as the
// second parameter to initialize, it will fall back
// to those nasty alerts, that can lead to very unfortunate
// situations dependending on which situations (events)
// you use them!

RAjax.Logger = Class.create({

  initialize: function(stream, performAlerts) {
    this.stream = (s = $(stream)) ? s : console; // maybe null
    this.performAlerts = performAlerts || false;
  },

  log: function(message) {
    if(PERFORM_LOGGING) {
      if(this.stream) {
        // Firebug console or HTMLElement present
        if(this.logsToFirebug()) {
          // Logging to Firebug console
          this.stream.log(message);
        } else {
          // Logging to HTMLElement
          this.stream.insert({bottom: this.formattedMessage(message)});
        }
      } else { // no stream present
        // fallback to annoying alerts
        alert(this.formattedMessage(message));
      }
    }
  },

  clear: function() {
    if(this.logsToPage()) {
      this.stream.update('');
    }
  },

  // override this if you need different formatting
  formattedMessage: function(message) {
    return "
  • ” + message + “
  • “; }, debug: function(message) { this.log(message); }, error: function(message) { this.log(”ERROR: ” + message); }, // utility methods logsToFirebug: function() { return !!this.stream.log; }, logsToPage: function() { return this.stream && !this.firebugPresent(); } }); // additional supported callbacks RAjax.EXTENDED_CALLBACKS = [ “onPOSTSuccess” , “onPOSTFailure”, “onGETSuccess” , “onGETFailure”, “onPUTSuccess” , “onPUTFailure”, “onDELETESuccess”, “onDELETEFailure” ];

    Read more »

    that = this;

    var Foo = Behavior.create({
    var that = this; // work around bug with this in inner functions
    this.foo = "bar";
    new Ajax.Request(url, {
    //...
    onSuccess: function(transport) {
    alert(that.foo); // this.foo would not work
    }
    })
    });

    lowpro autosave behavior to POST JSON

    Event.addBehavior({
      '.autosave': Behavior.create({
        onchange : function() {
          var source = this.element;
          var name = source.name.split('['); // name according to rails convention
          var model = name[0]; // rails model name
          var resource = model + 's'; // rails resource (TODO better pluralization)
          var id = name[1].substring(0, name[1].length - 1); // rails model id
    
          var authToken = 'authenticity_token=' + $('auth-token').getValue()
          if(name[1].match(/new/)) {
            var url = '/' + resource + '?' + authToken
          } else {
            var url = '/' + resource + '/' + id + '?_method=put&' + authToken;
          }
    
          var inputs = $(source.parentNode).id.split('-');
    
          new Ajax.Request(url, {
            method: 'post',
            contentType: "application/json",
            postBody: Object.toJSON({
              survey_execution_id: $('survey_execution_id').getValue(),
              question_application_input_id: inputs[1],
              time_line_offset: inputs[2],
              value: source.getValue()
            })
          });
        }
      });
    });

    ajax is just lipstick on a pig

    “Ajax is just lipstick on a pig”

    – Dave Thomas