Extend the PickupInstoreView
_.extend(PickupInStoreView.prototype, {
template: jj_pickupinstore_pickupinstore_tpl,
initialize: _.wrap(PickupInStoreView.prototype.initialize, function (fn) {
fn.apply(this, _.toArray(arguments).slice(1));
var self = this;
self.store_collection = new StoreLocatorCollection()
self.store_collection.fetch({
reset: true,
success: function (collection) {
// Find the default location by internalid or name
self.defaultLocation = collection.findWhere({ name: ‘Pharmacy’ }) || collection.get(4);
if (self.defaultLocation) {
console.log(‘Default location found:’, self.defaultLocation);
}
},
error: function (collection, response) {
console.error(‘Failed to fetch collection’, response);
}
});
self.store_collection.on(‘reset’, self.render, self);
}),
focusFirstInput: function () {
return !this.store_collection.length;
},
selectPickup: function (e) {
e.preventDefault();
var self = this;
// Set the default location to ‘Pharmacy’ if not already set
if (self.defaultLocation) {
this.model.set(‘location’, self.defaultLocation);
self.model.set(‘fulfillmentChoice’, ‘pickup’);
// Ensure the view is re-rendered to reflect the change
if (this.source && this.source === ‘cart’) {
LiveOrderModel.getInstance().updateLine(this.model);
}
this.render();
}
else {
if (this.getLocationStock() > 0) {
this.model.set(‘fulfillmentChoice’, ‘pickup’);
// If the source is the Cart, then the Live Order must be updated
if (this.source && this.source === ‘cart’) {
LiveOrderModel.getInstance().updateLine(this.model);
}
} else {
this.changeStore(e);
}
}
},
// Override the method responsible for showing the popup and prevent it
changeStore: function (e) {
// Prevent default popup behavior
e.preventDefault();
}
});