Skip to content

Instantly share code, notes, and snippets.

@stovak
Last active August 29, 2015 14:25
Show Gist options
  • Save stovak/f645b066861ad4b73934 to your computer and use it in GitHub Desktop.
Save stovak/f645b066861ad4b73934 to your computer and use it in GitHub Desktop.
'user strict';
var Cart = {};
(function($){
if (Cart) {
jQuery.extend(Cart, {
config: function(config) {
if (!this.config) {
this.config = config;
} else {
jQuery.extend(this.config, config);
}
return Cart.refresh().done(function(){
console.log(Cart);
return Cart;
});
},
addItem: function(options) {
console.log(this.config);
return jQuery.ajax({
url: this.config.baseUrl + "/api/rest/cart/items",
contentType: "application/json; charset=utf-8",
method: "POST",
data: JSON.stringify(options),
dataType: "json",
context: Cart,
xhrFields: {
withCredentials: true
},
headers: this.config.defaults.headers
}).then(function(data, textStatus, jqXHR){
return Cart;
}, Cart.errorHandler);
},
refresh: function() {
return jQuery.ajax({
url: this.config.baseUrl + "/api/rest/cart",
method: "GET",
dataType: "json",
context: Cart,
xhrFields: {
withCredentials: true
}
}).then(function(data,textStatus, jqXHR){
console.log(data);
jQuery.extend(Cart, data);
return Cart;
});
},
removeItem: function(options) {
console.log(options);
},
responseHandler: function(data, textStatus, jqXHR){
console.log("responseHandler");
var cookie = jqXHR.getResponseHeader("Set-Cookie");
console.log(cookie);
jQuery.extend(this, data);
console.log(this)
return Cart;
},
errorHandler: function(jqxhr, textStatus, errorThrown) {
try {
var error = JSON.parse(jqxhr.responseText);
console.log(error);
var message = "";
$.each(error.messages.error, function (key, value) {
message = message + value.message + "\n";
});
Cart.displayMessage(false, message);
} catch(e) {
console.log(e);
}
},
redirectHandler: function(jqXHR, textStatus, errorThrown) {
console.log("redirect");
var cookie = jqXHR.getResponseHeader("Set-Cookie");
console.log(cookie);
return Cart;
},
displayMessage: function(message) {
console.log(message);
}
});
}
}(jQuery))
(function($){
var config = YAML.load("tests/config/en_us.local.yml");
Cart
.config(config.magento_config);
Cart
.addItem({ "sku": "000-00023" });
Cart
.addItem({"sku": "000-00002"})
Cart
.refresh();
console.log(Cart);
})(jQuery);
@stovak
Copy link
Author

stovak commented Jul 23, 2015

First two addItem calls both have set-cookie in the response. Second call does not return the first's cookie. Second does not add the second cookie.

On refresh there's nothing in the cart.

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