FatFractal customer forums



Author Topic: Update event handler before update values  (Read 1348 times)

tomkif

  • Newbie
  • *
  • Posts: 9
    • View Profile
Update event handler before update values
« on: February 22, 2014, 09:25:33 AM »
Is there a way in the context of an UPDATE event to get the data before update as well as the data after update. This can permit me to update another object based on the previous value and to which value it will switch to:
« Last Edit: February 22, 2014, 09:33:02 AM by gkc »

gkc

  • Administrator
  • *****
  • Posts: 375
    • View Profile
Re: Update event handler before update values
« Reply #1 on: February 22, 2014, 09:36:07 AM »
Hi Tom,

Sure is.

Here’s a snippet which does some validation in order to prevent a field being changed in an object once it’s been correctly set. We've got an event handler, a server extension which demos the snippet working, and some FFDL

application.ffdl:
Code: [Select]
CREATE COLLECTION /ValidationDemo
CREATE HANDLER handleValidationDemoUpdate PRE on /ValidationDemo UPDATE AS javascript:require('scripts/ValidationDemo').handleValidationDemoUpdate();
CREATE EXTENSION /validationDemo UNSECURED as javascript:require('scripts/ValidationDemo').validationDemo();

ValidationDemo.js
Code: [Select]
var print = print;
var require = require;
var exports = exports;

var ff = require('ffef/FatFractal');

exports.handleValidationDemoUpdate = function() {
    var data = ff.getUpdateEventHandlerData();
    var oldObj = data['old'];
    var newObj = data['new'];

    if (newObj.yesOrNo) newObj.yesOrNo = newObj.yesOrNo.toUpperCase(); // force to upper-case

    if (oldObj.yesOrNo !== newObj.yesOrNo) { // value has changed
        if (oldObj.yesOrNo) { // There was a value there before, we’re not permitting changes
            throw {statusCode: 403, statusMessage: 'Once set, you may not change the value of yesOrNo'};
        }
        else { // it was null or undefined before, we’re setting for the first time - let’s validate
            if (newObj.yesOrNo !== 'YES' && newObj.yesOrNo !== 'NO') { // just an example
                throw {statusCode: 403, statusMessage: 'You must provide a Yes or No answer'};
            }
        }
    }
};

exports.validationDemo = function() {
    ff.deleteObjAtUri('/ValidationDemo/abcde');

    var obj = ff.createObjAtUri({clazz:'ValidationDemo',guid:'abcde'},'/ValidationDemo');

    obj.yesOrNo = "Foo";
    var allResults = [];

    var result = {action:'Updating to invalid value',shouldSucceed:false,succeeded:true};
    try { ff.updateObj(obj); } catch (Error) {result.succeeded = false;} // We expect this to fail
    allResults.push(result);

    obj.yesOrNo = 'yEs';
    result = {action:'Updating to valid value',shouldSucceed:true,succeeded:false};
    try { ff.updateObj(obj); result.succeeded = true;} catch (Error) {result.succeeded = false;} // We expect this to succeed
    allResults.push(result);

    result = {action:'Updating to same value',shouldSucceed:true,succeeded:false};
    try { ff.updateObj(obj); result.succeeded = true;} catch (Error) {result.succeeded = false;} // We expect this to succeed
    allResults.push(result);

    obj.yesOrNo = 'maybe';
    result = {action:'Updating to different, invalid value',shouldSucceed:false,succeeded:false};
    try { ff.updateObj(obj); result.succeeded = true;} catch (Error) {result.succeeded = false;} // We expect this to fail
    allResults.push(result);

    obj.yesOrNo = 'no';
    result = {action:'Updating to different, valid value',shouldSucceed:false,succeeded:false};
    try { ff.updateObj(obj); result.succeeded = true;} catch (Error) {result.succeeded = false;} // We expect this to fail
    allResults.push(result);

    ff.response().result = allResults;
    ff.response().wrap = false;
};

Output:
Code: [Select]
curl "http://localhost:8080/Samples/ff/ext/validationDemo" | underscore print --outfmt pretty
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   407  100   407    0     0   9064      0 --:--:-- --:--:-- --:--:--  9250
[
  {
    succeeded: false,
    shouldSucceed: false,
    action: "Updating to invalid value"
  },
  {
    succeeded: true,
    shouldSucceed: true,
    action: "Updating to valid value"
  },
  {
    succeeded: true,
    shouldSucceed: true,
    action: "Updating to same value"
  },
  {
    succeeded: false,
    shouldSucceed: false,
    action: "Updating to different, invalid value"
  },
  {
    succeeded: false,
    shouldSucceed: false,
    action: "Updating to different, valid value"
  }
]

 

Copyright © FatFractal customer forums