Skip to content

Instantly share code, notes, and snippets.

@skipjac
Last active December 16, 2015 07:38
Show Gist options
  • Save skipjac/5400085 to your computer and use it in GitHub Desktop.
Save skipjac/5400085 to your computer and use it in GitHub Desktop.
use to make sure all the fields in zendesk are available
//place these right after return
currAttempt : 0,
MAX_ATTEMPTS : 20,
events: {
'requiredProperties.ready': 'whatever'
}
//
init: function() {
this.requiredProperties = ['ticket.requester.email'];
this.allRequiredPropertiesExist();
},
// HELPER FUNCTIONS HELPER FUNCTIONS HELPER FUNCTIONS HELPER FUNCTIONS
allRequiredPropertiesExist: function() {
if (this.requiredProperties.length > 0) {
var valid = this.validateRequiredProperty(this.requiredProperties[0]);
// prop is valid, remove from array
if (valid) {
this.requiredProperties.shift();
}
if (this.requiredProperties.length > 0 && this.currAttempt < this.MAX_ATTEMPTS) {
if (!valid) {
++this.currAttempt;
}
_.delay(_.bind(this.allRequiredPropertiesExist, this), 100);
return;
}
}
if (this.currAttempt < this.MAX_ATTEMPTS) {
this.trigger('requiredProperties.ready');
} else {
services.notify('this is error');
}
},
safeGetPath: function(propertyPath) {
return _.inject( propertyPath.split('.'), function(context, segment) {
if (context == null) { return context; }
var obj = context[segment];
if ( _.isFunction(obj) ) { obj = obj.call(context);}
return obj;
}, this);
},
validateRequiredProperty: function(propertyPath) {
if (propertyPath.match(/custom_field/)) { return !!this.ticketFields(propertyPath); }
var value = this.safeGetPath(propertyPath);
return value != null && value !== '' && value !== 'no';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment