Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling method once but it executed many times in backbone
    primarykey
    data
    text
    <p>Here my class :</p> <pre><code>function Cart(){ if (typeof Cart.instance === 'object') { return Cart.instance; } Cart.instance = this; //the other method.... var self = this; var items = window.localStorage.getItem(Cart.storageName); this.addToCart = function(item){ if (!(items instanceof Array)) items = []; var itemIndex = getItemIndexById(items, item.ID); if(typeof(itemIndex) === 'number'){ items[itemIndex].QuantityInCart++; } else{ item.QuantityInCart = 1; items.push(item); } window.localStorage.setItem(Cart.storageName, serializeObjToJSON(items)); }; } Cart.storageName = "Cart"; </code></pre> <p>Then I call <code>addToCart</code> function in <code>Home</code> view when I click on <code>addToCart</code> button:</p> <pre><code>define(["jquery" , "underscore" , "backbone" , "text!templates/Home/homePanel.html", "text!templates/Item/itemTemplate.html" ],function($ , _ , Backbone , HomePanel, ItemTemplate){ var promotionItem = _.template(ItemTemplate); var homePanel = _.template(HomePanel); var HomeView = Backbone.View.extend({ initialize: function() { myCart1.updateQtyLabel("qtyCart"); window.localStorage.setItem("User",serializeObjToJSON(customer)); }, el: '#webbodycontainer', events : { "click #addToCart" : function(){ myCart1.addToCart(newItem); myCart1.updateQtyLabel("qtyCart"); $("#containernewpromotion").html(promotionItem); } }, render : function(){ this.$el.html(homePanel); $("#containernewpromotion").html(promotionItem); } }); return HomeView; }); </code></pre> <p>But when I click to the other view, then back to <code>Home</code> view, and click <code>addToCart</code> button again, the item is increasing 2 times (<code>addToCart</code> method executes two time). If I continue to another view and click on the button again , <code>addToCart</code> method executes 3 times.... Always +1 of <code>addToCart</code> method executing when I go to other view and come back to click on add to cart button.</p> <p>Any idea what could be causing this. Thanks. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload