-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshipping.js
228 lines (177 loc) · 7.14 KB
/
shipping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"use strict";
// ---------------------------------------------------------
// Security (well, not really security, but a helper to
// redirect the user if they're not logged in. Security
// is handled by the REST API.
// ---------------------------------------------------------
var redirectToLogin = function () {
var location_href = "index.html";
if (location.hash && location.hash.length > 0) {
location_href += "?hash=" + location.hash.substring(1);
}
location.href = location_href;
};
var theDocument = jQuery(document);
theDocument.ajaxSend(function (event, xhr) {
var cartId = getCookie('UltraCartShoppingCartID');
var merchantId = window.merchantId || getCookie('UltraCartMerchantID');
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("X-UC-Merchant-Id", merchantId);
xhr.setRequestHeader("X-UC-Shopping-Cart-Id", cartId);
});
theDocument.ajaxError(function (event, xhr) {
if (xhr.status === 401)
redirectToLogin();
});
jQuery.ajaxSetup({cache: false});
// ---------------------------------------------------------
// app skeleton
// ---------------------------------------------------------
var app = {
models: {}, // The M in MVC
collections: {}, // Also the M in MVC, just collections of them.
views: {}, // The V in MVC
templates: [],
zombieHunters: {}, // extensions that clean up event binding to avoid zombie event handlers
commonFunctions: {}, // functions that are used in numerous places
// the router is kind-of the C in MVC, but not in the traditional sense. It's more of a helper controller,
// which takes a look at the url (and fragments), and does additional things based on what it is.
// the router is created during document.ready
router: null,
data: {} // this will hold *instances* of collections and models. This is actual data, not definitions
};
// ---------------------------------------------------------------------
// --- zombie hunters (prevent orphaned event handlers)
// ---------------------------------------------------------------------
app.zombieHunters.appView = uc.commonFunctions.createAppView('addresses');
// ---------------------------------------------------------
// common functions
// ---------------------------------------------------------
// ---------------------------------------------------------
// data models
// ---------------------------------------------------------
app.models.Address = Backbone.Model.extend({
url: function () {
// restUrl is defined in myaccount_rest_X.X.js
return restUrl + '/shippingAddresses/' + this.get('id');
}
});
app.collections.Addresses = Backbone.Collection.extend({
model: app.models.Address,
// restUrl is defined in myaccount_rest_X.X.js
url: restUrl + '/shippingAddresses',
everBeenFetched: false
});
// an empty disconnected model used to prevent the same query from running multiple times simultaneously. (i.e. user clicks button numerous times)
app.models.StateManager = Backbone.Model.extend();
// ---------------------------------------------------------
// views
// ---------------------------------------------------------
// ---------------------------------------------------------------------
// --- Address ---
// ---------------------------------------------------------------------
app.views.Address = Backbone.View.extend({
tagName: 'article',
events: {
"click .editButton": 'editAddress',
"click .deleteButton": 'deleteAddress'
},
'onClose': function () {
},
initialize: function () {
_.bindAll(this);
},
render: function () {
// console.log('address ' + this.model.get('id') + ' render');
var context = this.model.attributes;
this.$el.html(app.templates.address(context));
this.$el.addClass('address');
return this;
},
'editAddress': function () {
location.href = "address.html?type=shipping&id=" + this.model.get('id');
},
'deleteAddress': function () {
if (confirm('Are you sure you want to delete this address?')) {
this.model.destroy();
}
}
});
// ---------------------------------------------------------------------
// --- Addresses ---
// ---------------------------------------------------------------------
app.views.Addresses = Backbone.View.extend({
tagName: 'div',
childViews: [],
events: {
"click .addButton": 'addAddress'
},
'onClose': function () {
this.collection.off('sync reset change remove', this.render, this);
this.closeChildren();
// dispose of the children
_.each(this.childViews, function (view) {
view.close();
});
},
initialize: function () {
this.collection.on('sync reset change remove', this.render, this);
_.bindAll(this);
},
render: function () {
// console.log('orders.render');
var context = null;
if (this.collection.everBeenFetched) {
context = {
'loading': false
};
} else {
context = {
'loading': true
};
}
this.$el.html(app.templates.addresses(context));
var that = this;
// the first time, we don't need to do any clean up. but subsequent render() calls need to close down any
// existing views to avoid zombie event handlers.
this.closeChildren();
this.childViews = []; // re-init on each render so we capture any and all changed.
var counter = 1;
this.collection.each(function (model) {
model.set({'position': counter}, {silent: true});
counter++;
that.childViews.push(new app.views.Address({model: model}));
});
var ordersDiv = jQuery('#addresses', this.el);
_.each(this.childViews, function (view) {
view.render();
ordersDiv.append(view.el);
});
return this;
},
'closeChildren': function () {
_.each(this.childViews, function (view) {
view.close();
});
},
'addAddress': function () {
location.href = "address.html?type=shipping";
}
});
jQuery(document).ready(function () {
enablePleaseWaitMessage();
app.data.stateManager = new app.models.StateManager();
app.data.addresses = new app.collections.Addresses();
app.templates.address = Handlebars.compile(jQuery('#address-template').html());
app.templates.addresses = Handlebars.compile(jQuery('#addresses-template').html());
var addressesView = new app.views.Addresses({'collection': app.data.addresses});
app.data.addresses.everBeenFetched = false; // reset the fetch so the 'please wait' shows again.
app.zombieHunters.appView.showView(addressesView);
// everBeenFetched is a collection property used to distinguish between an initial page load and when there are no records
// returned from the server. The former should show 'please wait, loading records', the latter should show 'no records found'
app.data.addresses.fetch({
success: function (collection) {
collection.everBeenFetched = true;
}
});
});