1(function (app, $, Backbone, _, utils) { 2 'use strict'; 3 4 // ================================= 5 var AppStateModel = Backbone.Model.extend((function () { 6 7 // ================================= 8 // Private stuff here 9 // 10 var _init = false; 11 12 // ================================= 13 return { 14 15 // ================================= 16 defaults: { 17 active_primary_group_id: null, 18 active_bill_group_id: null, 19 20 // Disallow setting the below - they're driven by the above 21 // 22 active_primary_group: null, 23 active_primary_group_accounts: [], 24 active_primary_group_bill_groups: [], 25 active_bill_group: null, 26 active_bill_group_accounts: [], 27 active_bill_group_statements: new app.StatementCollection() 28 }, 29 30 // ================================= 31 setActives: function (active_primary_group_id, active_bill_group_id, options) { 32 33 34 active_primary_group_id = active_primary_group_id || this.get('active_primary_group_id'); 35 36 active_bill_group_id = active_bill_group_id || this.get('active_bill_group_id'); 37 38 39 var active_primary_group = null; 40 var active_primary_group_accounts = []; 41 var active_primary_group_bill_groups = []; 42 var active_bill_group = null; 43 var active_bill_group_accounts = []; 44 var active_bill_group_statements_array = []; 45 46 var allPrimaryGroups = this.model.get('all_primary_groups') || []; 47 48 if (allPrimaryGroups.length > 0) { 49 50 if (active_primary_group_id) { 51 52 active_primary_group = _.where(allPrimaryGroups, { account_group_id: active_primary_group_id })[0]; 53 } 54 55 if (!active_primary_group) { 56 active_primary_group = allPrimaryGroups[0]; 57 } 58 59 if (active_primary_group) { 60 active_primary_group_id = active_primary_group.account_group_id; 61 62 active_primary_group_accounts = _.where(this.model.get('all_accounts'), { primary_account_group_id: active_primary_group_id }); 63 active_primary_group_bill_groups = _.where(this.model.get('all_billing_groups'), { primary_account_group_id: active_primary_group_id }); 64 } else { 65 active_primary_group_id = null; 66 active_primary_group_accounts = []; 67 active_primary_group_bill_groups = []; 68 } 69 } 70 71 if (active_primary_group_bill_groups.length > 0) { 72 73 if (active_bill_group_id) { 74 active_bill_group = _.where(active_primary_group_bill_groups, { account_group_id: active_bill_group_id })[0]; 75 76 } 77 78 if (!active_bill_group) { 79 active_bill_group = active_primary_group_bill_groups[0]; 80 } 81 82 if (active_bill_group) { 83 active_bill_group_id = active_bill_group.account_group_id; 84 active_bill_group_accounts = _.where(this.model.get('all_accounts'), { billing_account_group_id: active_bill_group_id }); 85 active_bill_group_statements_array = _.where(this.model.get('all_statements'), { account_group_id: active_bill_group_id }); 86 } else { 87 active_bill_group_id = null; 88 active_bill_group_accounts = []; 89 active_bill_group_statements_array = []; 90 } 91 } 92 93 // Can't overwrite in set.call() 94 // 95 this.get('active_bill_group_statements').reset(active_bill_group_statements_array); 96 97 return Backbone.Model.prototype.set.call(this, { 98 active_primary_group_id: active_primary_group_id, 99 active_bill_group_id: active_bill_group_id, 100 active_primary_group: active_primary_group, 101 active_primary_group_accounts: active_primary_group_accounts, 102 active_primary_group_bill_groups: active_primary_group_bill_groups, 103 active_bill_group: active_bill_group, 104 active_bill_group_accounts: active_bill_group_accounts 105 }, options); 106 }, 107 108 // ================================= 109 initialize: function (options) { 110 111 this.model = options.model; 112 113 var that = this; 114 this.listenTo(this.model, 'change:all_primary_groups', function () { that.setActives(null, null); }); 115 this.listenTo(this.model, 'change:all_billing_groups', function () { that.setActives(null, null); }); 116 117 _init = true; 118 }, 119 120 // ================================= 121 // Override Backbone.set() 122 // 123 set: function (key, value, options) { 124 125 // Normalize 126 // 127 var attrs = null; 128 129 if (_.isObject(key) || key === null) { 130 131 attrs = key; 132 options = value; 133 } 134 else { 135 attrs = {}; 136 attrs[key] = value; 137 } 138 139 if (_init === false) { 140 return Backbone.Model.prototype.set.call(this, attrs, options); 141 142 } 143 144 var active_primary_group_id = this.get('active_primary_group_id'); 145 var active_bill_group_id = this.get('active_bill_group_id'); 146 147 for (var attr in attrs) { 148 if (attr === 'active_primary_group_id') 149 active_primary_group_id = attrs[attr]; 150 else if (attr === 'active_bill_group_id') 151 active_bill_group_id = attrs[attr]; 152 else 153 throw new Error(attr + ' cannot be set'); 154 } 155 156 return this.setActives(active_primary_group_id, active_bill_group_id, options); 157 }, 158 }; 159 })()); 160 161 // ================================= 162 var UserModel = Backbone.Model.extend({ 163 164 defaults: { 165 party: null, 166 all_primary_groups: [], 167 all_billing_groups: [], 168 all_accounts: [], 169 all_statements: [], 170 app_state: null, 171 // companies: {}, 172 advocacyCentreUnlimitedItems: [], 173 advocacyCentrePremiumItems: [] 174 }, 175 176 // ================================= 177 initialize: function (options) { 178 this.set('app_state', new AppStateModel({ model: this })); 179 }, 180 181 // ================================= 182 // Called on model.fetch(), model.save() and model.destroy(). Emits 'request', then either 'sync' or 'error' 183 // 184 sync: function (method, model, options) { 185 186 switch (method) { 187 188 case "read": // Called on on model.fetch() 189 return app.common.wrappedSync(method, model, options, 'get_user'); 190 191 /* 192 case "update": // Called on on model.save() 193 case "patch": // Called on on model.save({patch: true}) - only passes changed values to server 194 case "delete": // Called on on model.destroy() 195 case "create": // not called for models 196 */ 197 198 default: 199 throw new Error('UserModel:sync(): method not implemented'); 200 } 201 }, 202 203 // ================================= 204 // Called on successful model sync. Map from server response to desired model attributes
205 // TODO?: would like to check resp.success here, rather than in app.common.wrappedSync() but can't seem to 206 // trigger model.fetch().fail() at all, e.g. throwing exception or invoking options.error() 207 // 208 parse: function (resp, options) { 209 210 // all_primary_groups - all comm/resi account groups for this party 211 // all_billing_groups - corresponding to comm/resi account groups (all_primary_groups) 212 // all_accounts - the accounts in all comm/resi groups (all_primary_groups) 213 214 var data = { 215 party: utils.getArrayFromCFQuery(resp.data.PARTYQRY)[0], 216 all_primary_groups: utils.getArrayFromCFQuery(resp.data.ACCOUNTGROUPSQRY), 217 all_billing_groups: utils.getArrayFromCFQuery(resp.data.ALLBILLINGACCOUNTGROUPSQRY), 218 all_accounts: utils.getArrayFromCFQuery(resp.data.ALLACCOUNTSQRY, { BILLED_UNTIL_DATE: 'datetime' }), 219 all_statements: utils.getArrayFromCFQuery(resp.data.ALLSTATEMENTSQRY, { STATEMENT_DATE: 'datetime', DUE_DATE: 'datetime' }), 220 // all_primary_groups_emissions: resp.data.EMISSIONS.BY_PRIMARY_GROUP, 221 // all_accounts_emissions: resp.data.EMISSIONS.BY_ACCOUNT, 222 // companies: resp.data.COMPANIES, 223 advocacyCentreUnlimitedItems: resp.data.ADVOCACYCENTREUNLIMITEDITEMS, 224 advocacyCentrePremiumItems: resp.data.ADVOCACYCENTREPREMIUMITEMS 225 }; 226 227 console.log('UserModel:parse(): model is', data); 228 return data; 229 } 230 }); 231 232 // ================================= 233 app.UserModel = UserModel; 234 235})(window.mybf, jQuery, Backbone, _, bullfrog.utils);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.