Posts Tagged ‘lowpro’

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()
        })
      });
    }
  });
});