javascript - Backbone update model if its already exist in collection -


i'm trying implement basic cart on backbone.js i'm new in it.itemslistview adds object cartcollection. problem when model added in collection want increment model quantity attribute if model exist in cartcollection.

 var phone = backbone.model.extend({}); var phonescollection = backbone.collection.extend({     model: phone }); var itemlistview = backbone.view.extend({      collection: null,     _template: _.template($('#listtemplate').html()),     el: $('#phonesdiv'),     events: {         'click .buybutton': '_addtocart'     },     initialize: function () {         'use strict';         this.render();     },     render: function () {         'use strict';         var rendtemplate = this._template({items: this.collection.tojson()});         this.$el.html(rendtemplate);         return this;     },     _addtocart: function (e) {         'use strict';         var buttonid = $(e.currenttarget).attr('id');         var result = this.collection.findwhere({id: buttonid});         var purchase = {             id: result.attributes.id,             name: result.attributes.name,             price: result.attributes.price         };         cartcollection.add(new cartmodel({             id: buttonid,             item: _.pick(purchase, 'id', 'name', 'price'),             itemtotalprice: purchase.price         }));         console.log(cartcollection);     } }); 

cartmodel , cartcollection:

var cartmodel = backbone.model.extend({      defaults: {         id: null,         item: {             id: null,             name: null,             price: null         },         itemtotalprice: 0,         quantity: 1     } }); var cartcollection = backbone.collection.extend({     model: cartmodel,     defaults:{         totalquantity: 0,         totalprice: 0     } 

you can adding method collection class. here's 1 way it, method i'm calling addtocart:

var cartmodel = backbone.model.extend({   defaults: {     quantity: 0   } });  var cartcollection = backbone.collection.extend({   model: cartmodel,   addtocart: function (model) {     this.add(model);     var q = model.get('quantity');     model.set('quantity', q + 1);   } }); 

when call backbone collection's add method, if model use argument in collection, not added again. then, can increment model's quantity manually.

the code below shows how work; can play in jsbin.

var m1 = new cartmodel({ name: 'm1' }); var m2 = new cartmodel({ name: 'm2' });  var cart = new cartcollection();  cart.addtocart(m1); cart.addtocart(m1); cart.addtocart(m2);  console.log('cart length:', cart.length); // 2  console.log('m1 quantity:', m1.get('quantity')); // 2 console.log('m2 quantity:', m2.get('quantity')); // 1 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -