Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of a SugarCRM sidecar Plugin that "mixes in" a keydown event listener for all postal code fields and makes a call to an Sugar REST endpoint to retrieve City name
(function (app) {
app.events.on("app:init", function () {
/**
* This plugin allows views that contain *_postalcode attributes
* to be wired up with a keypress event that will contact a postal-code validator
* service
*
* <pre><code>
* {
* //Sample component
* plugins: ['PostalCodeValidator'],
*
* }
* </code></pre>
*/
app.plugins.register('PostalCodeValidator', 'view', {
/**
* Locate all *_postalcode fields and attach a keydown event handler
*/
onAttach: function () {
app.logger.info('PostalCodeValidator: Inside the onAttach method');
this.events = (this.events) ? this.events : {};
_.extend(this.events, {
"keydown input[name*='_postalcode']" : _.bind(function (e) {
if (!this.disposed) {
//Trigger call to api when the "Enter"
//key has been entered
if (e.key === 'Enter') {
var currentPostalCode = this.$el.find("input[name='" + e.target.name + "']").val();
app.logger.info('Calling API with the value: ' + currentPostalCode);
var restURL = app.api.buildURL('CityValidator') + '/' + currentPostalCode + '/' +
e.target.name;
app.logger.info(restURL);
app.api.call('read', restURL, {}, {
success: _.bind(this._handleSuccess, this),
error : _.bind(this._handleError, this)
});
} else {
app.logger.info('The key typed in is: ' + e.key);
}
}
}, this)
});
},
_handleSuccess : function (data) {
if (this.disposed) {
return;
}
//use the postcode_attr prop to determine the City attribute
var postCodeAttr = data.postcode_attr;
//the assumption is that all postcode fields end with "_postalcode" in the name
//Let's strip this off and use what's left to infer the City
var postalCodeIndex = postCodeAttr.indexOf('_postalcode');
if (postalCodeIndex > 0) {
//save the substring from 0 to postalCodeIndex + 1 (Why postalCodeIndex + 1? To keep the underscore
//in the resulting substring)
var baseAttr = postCodeAttr.substr(0, postalCodeIndex + 1);
//let's add city to the end of the base attribute
var cityAttr = baseAttr + 'city';
//Does this exist on our Model? If so, then set it. If not, then do nothing or maybe throw an Alert?.
if (_.has(this.model.fields, cityAttr)) {
this.model.set(cityAttr, data.city);
}
}
},
_handleError : function (data) {
app.logger.info('Data param is: ' + data);
if (this.disposed) {
return;
}
app.logger.info('Leaving the error handler');
}
});
});
})(SUGAR.App);
// noinspection BadExpressionStatementJS
({
extendsFrom: 'AccountsRecordView',
/**
* @inheritDoc
*/
initialize: function (options) {
SUGAR.App.logger.info('Inside the initialize method of my custom AccountsRecordView controller');
//Add the postcode validator plugin to the list of existing plugins
this.plugins = _.union(this.plugins || [], ['PostalCodeValidator']);
SUGAR.App.logger.info('Custom View After the addition of PostalCodeValidator');
this._super('initialize', [options]);
SUGAR.App.logger.info('Custom Accounts View, after call to the parent initialize method');
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.