Skip to content
This repository has been archived by the owner on Aug 30, 2018. It is now read-only.

Add events to notify of drawer opening / closing #481

Merged
merged 15 commits into from
May 26, 2016

Conversation

humancopy
Copy link
Contributor

Hey :)

I think it's a good idea to start using events in Timber instead of callbacks to make it easy to tap into interface changes. I also want to add events to the ajax_cart.js file, what you think?

@cshold
Copy link
Contributor

cshold commented Mar 1, 2016

I agree this is a great approach. Are you able to update the docs to include these as well?

@humancopy
Copy link
Contributor Author

Yes, would be happy to do it. I'll add it to this pull request. Couldn't find where to update the docs. :)

@humancopy
Copy link
Contributor Author

I added more triggers and fixed the namespace. I've implemented this in a few projects and it's been working great.

Let me know what we can do to push this forward, I'm happy to help with anything needed.

Have a beautiful weekend.

Peace ;)

@Cam
Copy link

Cam commented May 15, 2016

Awesome. This is exactly what I was talking about a little while back. Thanks heaps @humancopy 😃

@@ -72,18 +78,23 @@ ShopifyAPI.onError = function(XMLHttpRequest, textStatus) {
- Allow custom error callback
==============================================================================*/
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) {
var params = {
var $body = $('body'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a nitpick, but can you change this to $(document.body) - it's slightly faster (source).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, changed it in a couple of other places also ;)

@@ -325,7 +357,7 @@ var ajaxCart = (function(module, $) {

cartCallback = function(cart) {
$body.removeClass('drawer--is-loading');
$body.trigger('ajaxCart.afterCartLoad', cart);
$body.trigger('afterCartLoad.ajaxCart', cart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this name. It's the only breaking change I can see in the PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that. I'll update the version number differently instead. All good

jQuery.getJSON('/cart.js', function (cart, textStatus) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$(document.body).trigger('afterGotCart.ajaxCart', cart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last change, switch this to afterGetCart. Realizing the naming issues as I'm writing up some new docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, done... Let me know if need any help with docs. :)

@@ -354,6 +386,7 @@ var ajaxCart = (function(module, $) {

// If it has a data-line, update the cart.
// Otherwise, just update the input's number
$body.trigger('adjustCart.ajaxCart', [this, line, qty]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove this. updateQuantity (the function right after this) calls ShopifyAPI.changeItem, which already has triggers fired (before/after/error/complete ChangeItem)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateQuantity is not always getting triggered which is why I put this one here

@cshold
Copy link
Contributor

cshold commented May 20, 2016

Alright, we're almost there.

Let's remove:

  • beforeSubmit.ajaxCart | duplicate of event in ShopifyAPI.addItemFromForm
  • afterSubmit.ajaxCart | duplicate, same as above
  • itemError.ajaxCart | duplicate, ShopifyAPI.addItemFromForm triggers errorAddItem.ajaxCart regardless if an error callback is sent
  • adjustCart.ajaxCart | duplicate of events triggered in ShopifyAPI.changeItem
    • My argument with this one is that if ShopifyAPI.changeItem isn't called, the events aren't needed at all. Every other event is tied to an ajax call except this one.

@cshold cshold mentioned this pull request May 20, 2016
@humancopy
Copy link
Contributor Author

Removed redundant events. Docs look really good, well done! :)

@cshold
Copy link
Contributor

cshold commented May 25, 2016

Thanks for the updates. Did a bunch of testing and it looks in order. The only duplicate events triggered are afterCartLoad.ajaxCart and afterGetCart.ajaxCart — but I'm happy to leave both. The second one is in the ShopifyAPI.getCart function itself, firing no matter what callback is sent, while the first one is specific to Timber's drawer implementation.

Will give the docs a quick review the merge both. Cheers to putting this together.

@cshold
Copy link
Contributor

cshold commented May 25, 2016

Last ask, could you squash the commits down to one. Here are the steps.

@humancopy
Copy link
Contributor Author

Unfortunately I can't squash because I merged stuff from the Shopify/Timber as I needed to keep it up to date cause I was using it for some projects, sorry. If you have an idea how to resolve this I'm happy to follow... ;)

@cshold
Copy link
Contributor

cshold commented May 25, 2016

No worries, more of a nice to have than anything.

@cshold cshold merged commit 0f43c66 into Shopify:master May 26, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants