Callbacks with JS requirement

So I am trying to make two functions that allow the user to move an item from their cart to the "saved cart" and vice versa. These functions depend on the Cart Group Item module, which also contains events for button clicks. My question is that I am not sure how to properly call these functions so that the click event occurs in my current js file. Hope someone can help!

Module event:

cartGroupItem.prototype.createMoveEvent = function (elem) {
  if (undefined !== elem && null !== elem) {
    var button = elem.querySelector('.cartGroupItem__move');
    if (button !== null) {
      button.addEventListener('click', function (e) {
        ajax('GET',
               '/resources/ajax/cart.aspx?sub=3&Saved=0&Cart_Group_ID='+this.cartGroupId,
          true, {}, function () {
            this.moveToCartCallback();
          }.bind(this), function () {
            this.showErrorDiv();
          }.bind(this));
      }.bind(this));
    }
  }
};

cartGroupItem.prototype.createSaveEvent = function (elem) {
  if (undefined !== elem && null !== elem) {
    var button = elem.querySelector('.cartGroupItem__save');
    if (button !== null) {
      button.addEventListener('click', function (e) {
        ajax('GET',
               '/resources/ajax/cart.aspx?sub=3&Saved=1&Cart_Group_ID='+this.cartGroupId,
          true, {}, this.saveForLaterCallback.bind(this), this.showErrorDiv.bind(this));
      }.bind(this));
    }
  }
};

      

Moving functions:

function moveToSaved(cartGroupId) {
    for (var i = 0, l = activeCartList.length; i < l; i++) {
        if (activeCartList[i].cartGroupId === cartGroupId) {
            activeCartList.remove();
            savedCartList.push(activeCartList[i]);
        }

    }
}

function moveToActive(cartGroupId) {
    for (var i = 0, l = savedCartList.length; i < l; i++) {
        if (savedCartList[i].cartGroupId === cartGroupId) {
            savedCartList.remove();
            activeCartList.push(savedCartList[i]);
        }
    }
}

      

+3


source to share


1 answer


Perhaps your module Event

is defining the function cartGroupItem

correctly?

What you need to do is pass this function from your file to your current file, and then take away the carteGroupItem file:

// In your current JS file
var myCartGroupItem = new cartGroupItem();
myCartGroupItem.createMoveEvent();
myCartGroupItem.createSaveEvent();

      

We will also need to see this "constructor" function (where it is defined), since it probably takes multiple callbacks as parameters. Otherwise, you can add them manually:



myCartGroupItem.moveToCartCallback = function() {
  // do what you want when the item is moved to cart
};
// Same for the other callbacks
myCartGroupItem.saveForLaterCallback = function() { ... };
myCartGroupItem.showErrorDiv = function() { ... };

      

Finally, the way to pass things around with RequireJS is what, for example, your module Event

returns cartGroupItem

, so in your file module:

define(['Event'], function(cartGroupItem) {
  // Your code where you can use cartGroupItem
});

      

+2


source







All Articles