Skip to content

Instantly share code, notes, and snippets.

@logicaroma
Last active August 29, 2015 13:57
Show Gist options
  • Save logicaroma/9504414 to your computer and use it in GitHub Desktop.
Save logicaroma/9504414 to your computer and use it in GitHub Desktop.
/*
saves the configuration of all accounts to the server.
*/
saveSettings: function() {
var accounts = this.get('Accounts'),
payload = [];
//process each account and add the items to the payload
accounts.forEach(function (item) {
// pick the needed keys for the payload
item = _.pick(item, ['IBAN', 'Currency', 'Configuration']);
//add ShowProduct key if missing
_.defaults(item.Configuration, { ShowProduct: true });
payload.push(item);
});
this.save({}, {
data: {
'Accounts': {
'Account': payload
}
},
error: function(model) {
model.trigger('product:error:save', { message: 'de data kan niet worden opgeslagen.'});
model.fetch();
},
success: function(model) {
model.fetch();
}
});
},
/*
update the account configuration object with the suplied name as the CustomProductName
*/
setCustomProductName: function (iban, name) {
var isValid = false,
validationPattern = new RegExp('^[a-zA-Z0-9.-/ ?:(),+\']*$');
if(iban && name){
isValid = validationPattern.test(name);
if( isValid ){
this.setAccountConfiguration(iban, {CustomProductName: name});
} else {
this.trigger('product:error:customproductname', { message: 'Verwijder de ongeldige tekens', data: { iban: iban, name: name }});
}
}
},
/*
update the ordering for each account.
ordering array should contain objects with an iban and a sortingOrder
{
iban: 'IBAN',
sortingOrder: {}
}
*/
setSortingOrder: function(ordering) {
if(ordering && Array.isArray(ordering)){
ordering.forEach(function(item) {
this.setAccountConfiguration(item.iban, { SortingOrder: item.sortingOrder });
}.bind(this));
}
},
/*
returns an array containing the sorting order
*/
getSortingOrder: function() {
var accounts = this.getSortedAccounts(),
ordering =[];
accounts.forEach(function(account) {
if (account.Configuration && account.Configuration.SortingOrder) {
ordering.push(account.Configuration.SortingOrder);
} else {
ordering.push(accounts.indexOf(account));
}
}, this);
return ordering;
},
/*
**private**
update the accounts configuration object with the suplied CustomProductName
data object should contain an IBAN and a CustomProductName
*/
setAccountConfiguration: function(iban, configuration){
var accounts = this.get('Accounts'),
accountIndex = _.findIndex(accounts, {
'IBAN': iban
}),
newConfiguration = _.extend(accounts[accountIndex].Configuration, configuration);
accounts[accountIndex].Configuration = newConfiguration;
this.set('Accounts', accounts, { silent: true });
},
@mpoelstra
Copy link

voor de regex heb ik lokaal het eea veranderd:

setCustomProductName: function (data) {
            debugger;
            var accounts = this.get('Accounts'),
                accountIndex,
                pattern,
                isValid = false;

            if(data.CustomProductName && data.IBAN){
                pattern = new RegExp('^[a-zA-Z0-9.-/ ?:(),+\']*$');

                isValid = pattern.test(data.CustomProductName);
                if( isValid ){
                    accountIndex = _.findIndex(accounts, {
                        'IBAN': data.IBAN
                    });
                    accounts[accountIndex].Configuration.CustomProductName = data.CustomProductName;
                    this.set('Accounts', accounts);

                } else {
                    this.trigger('error:customproductname',{message: 'Verwijder de ongeldige tekens', data: data});
                }
            }
        },

@BossEdWork
Copy link

    isAccountInputValid: function(event) {
        var pattern = '^[a-zA-Z0-9.-/ ?:(),+\']*$';

        return (event.currentTarget.value.search(pattern) === 0);
    },

@logicaroma
Copy link
Author

/*
        update the account configuration object with the suplied name as the CustomProductName
        */
        setAccountCustomProductName: function (iban, name) {
            var accounts = this.get('Accounts'),
                accountIndex,
                isValid = false,
                validationPattern = new RegExp('^[a-zA-Z0-9.-/ ?:(),+\']*$');

            if(iban && name){

                isValid = validationPattern.test(name);
                if( isValid ){
                    accountIndex = _.findIndex(accounts, {
                        'IBAN': iban
                    });
                    accounts[accountIndex].Configuration.CustomProductName = name;
                    this.set('Accounts', accounts, { silent: true });

                } else {
                    this.trigger('error:customproductname',{message: 'Verwijder de ongeldige tekens', data: { iban: iban, name: name }});
                }
            }
        },

@mpoelstra
Copy link

setCustomProductName: function (iban, name) {
            var isValid = false,
                validationPattern = new RegExp('^[a-zA-Z0-9.-/ ?:(),+\']*$');

            if(iban && name){

name can be empty (when resetting/removing the customproductname, I also call this function now), so change this into if (iban){

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment