Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:02:25+00:00 2026-06-01T00:02:25+00:00

This code of mine keeps on throwing error i don’t understand why. var id

  • 0

This code of mine keeps on throwing error i don’t understand why.

var id = $(ev.currentTarget).data("id");
    var item = ItemCollection.getByCid(id);
    alert(item.get("ItemCode"));
    var qty = 1;
    var cartcollection = new CartCollection();
    cartcollection.add( item );
    CartListView.render();
    var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")});
    if( itemcode.length > 0 ){ alert("success"); }

So what i want to do is check if the CartCollection has the same model already and if true it should update the Qty atrib of the model only. Now based on that code it returns CartCollection is not a function or not a constructor. Why the hell is that!? Any ideas? Thanks

Update

I’m using backbone,require,KendoUI grid and underscore on this one so my code is this:

itemlist_view.js

define([
'jquery',
'underscore',
'backbone',
'model/item_model',
'model/cart_model',
'collection/item_collection',
'collection/cart_collection',
'view/cart/cartlist_view',
'text!templates/items/itemlist.html'
 ],function($, _, Backbone, Item, Cart, ItemCollection, CartCollection, CartListView, ItemListTemplate){

var ItemListView = Backbone.View.extend({
el: $("#mainContainer"),
events:{
    "click #itemListContainer li" : "AddToCart"
},
initialize: function(){
  this.model = Item;
  this.collection = ItemCollection;
  this.collection.bind("reset", this.render );
},
render: function(){
  var data = {
    items: ItemCollection.models
  }
  var compiledTemplate = _.template( ItemListTemplate , data);
  $("#itemContainer").html( compiledTemplate );
},
AddToCart:function(ev){
    ev.preventDefault();
    var id = $(ev.currentTarget).data("id");
    var item = ItemCollection.getByCid(id);
    alert(item.get("ItemCode"));
    var qty = 1;
    var cartcollection = new CartCollection();
    cartcollection.add( item );
    CartListView.render();
    var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")});
    if( itemcode.length > 0 ){ alert("success"); }
}
});
return new ItemListView;
});

cart_collection.js

define([
'underscore',
'backbone',
'model/cart_model'
],function(_, Backbone, Cart){
var CartCollection = Backbone.Collection.extend({
    model: Cart,
  initialize: function(){

  }
});
return new CartCollection;
 });

cartlist_view.js

define([
 'jquery',
 'underscore',
 'backbone',
 'model/cart_model',
 'collection/cart_collection',
 'text!templates/cart/cartlist.html'
 ], function($, _, Backbone, Cart, CartCollection, CartListTemplate){

var Model = kendo.data.Model,
    ObservableArray = kendo.data.ObservableArray;

function wrapBackboneModel(backboneModel, fields) {
    return Model.define({
        fields: fields,
        init: function(model) {
            if (!(model instanceof backboneModel)) {
                model = new backboneModel(model);
            }

            Model.fn.init.call(this, model.toJSON());
            this.backbone = model;
        },
        set: function(field, value) {
            Model.fn.set.call(this, field, value);

            this.backbone.set(field, value);
        }
    });
}

function wrapBackboneCollection(model) {
    return ObservableArray.extend( {
        init: function(collection) {
            ObservableArray.fn.init.call(this, collection.models, model);

            this.collection = collection;
        },

        splice: function(index, howMany) {
            var itemsToInsert, removedItemx, idx, length;

            itemsToInsert = Array.prototype.slice.call(arguments, 2);

            removedItems = kendo.data.ObservableArray.fn.splice.apply(this, arguments);

            if (removedItems.length) {
                for (idx = 0, length = removedItems.length; idx < length; idx++) {
                    this.collection.remove(removedItems[idx].backbone);
                }
            }

            if (itemsToInsert.length) {
                for (idx = 0, length = itemsToInsert.length; idx < length; idx++) {
                    this.collection.unshift(itemsToInsert[idx].backbone);
                }
            }

            return removedItems;
        }
    });
}

kendobackboneCollection = wrapBackboneCollection;
kendobackboneModel = wrapBackboneModel;


var CartListView = Backbone.View.extend({
el: $("#cartContainer"),

initialize: function(){
  this.collection = CartCollection;
  this.model = Cart;
  this.collection.bind("change", this.render );
},
render: function(){
  console.log("here");
  this.el.html(CartListTemplate);
  var CartWrapper = kendobackboneModel(Cart, {
     ItemCode: { type: "string" },
     ItemDescription: { type: "string" },
     RetailPrice: { type: "string" },
     Qty: { type: "string" },
  });
  var CartCollectionWrapper = kendobackboneCollection(CartWrapper);

  this.$("#grid").kendoGrid({
    editable: true,
    toolbar: ["create"],
    columns: ["ItemDescription", "Qty", "RetailPrice"],
    dataSource: {
      schema: {model: CartWrapper},
      data: new CartCollectionWrapper(CartCollection),
     }
  });
  
},

});
return new CartListView;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:02:27+00:00Added an answer on June 1, 2026 at 12:02 am

    I think the problem is that you are double instantiating the CartCollection. That is, cart_collection.js returns a new CartCollection() and in itemlist_view.js you instantiate it again with var cartcollection = new CartCollection();.

    Change that line to var cartcollection = CartCollection; and see how that does. Obviously you could also remove the cartcollection variable and replace its usages with CartCollection.

    On another note, I would seriously consider NOT returning new CartCollection() from your cart_collection.js. This feels like a very bad practice in that you will only ever be able use one instance of that collection. Also, it isn’t obvious to the developer that they are getting back an instance of the collection rather than the constructor for it.

    I would recommend returning CartCollection instaed, and then instantiating it in your itemlist_view.js file with new CartCollection(). This way you will be able to instantiate more of those collections later if needed.

    Same recommendation for cartlist_view.js returning a new CartListView().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend of mine sent me this code snippet to celebrate his new baby
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
This question is related to a previous question of mine That's my current code
I have the following code in a class of mine. The purpose of this
I have this code : for (var i = 0; i < result.length; i++)
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.