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

  • SEARCH
  • Home
  • 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 8967321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:13:00+00:00 2026-06-15T17:13:00+00:00

So I am trying storing product types from a json file before trying to

  • 0

So I am trying storing product types from a json file before trying to add them to a collection but am getting some strange results (as in I dont fully understand)

on my router page i setup a variable for cached products as well as product types

cachedProductTypes: null,
productType : {}, 
products : {},  

getProductTypes:
        function(callback)
        {
            if (this.cachedProductTypes !== null) {
                return callback(cachedProductTypes);
            }

            var self = this;

            $.getJSON('data/product.json',
                function(data)
                {
                    self.cachedProductTypes = data;
                    callback(data);
                }
            );
        },

parseResponse : function(data) {


          result = { prodTypes: [], products: [] };

          var type;
          var types = data.data.productTypeList;
          var product;
          var i = types.length;


         while (type = types[--i]) {
            result.prodTypes.push({ 
               id: type.id,
               name: type.name,
               longName: type.longName
               // etc.
            });
            while (product = type.productList.pop()) {
              product.productTypeId = type.id,
              result.products.push(product);
            }   
          };

          this.productType = result.prodTypes;
          console.log( "dan");
          this.products = result.products;      
    },
showProductTypes:function(){
        var self = this;

        this.getProductTypes(
            function(data)
            {
                self.parseResponse(data);

     var productTypesArray = self.productType;
     var productList=new ProductsType(productTypesArray);   
     var productListView=new ProductListView({collection:productList});

     productListView.bind('renderCompleted:ProductsType',self.changePage,self); 
     productListView.update();
    }
        );      
    }

when a user goes to the show product types page it runs the showProductsType function

So I am passing the products type array to my collection

on the collection page

      var ProductsType=Backbone.Collection.extend({

      model:ProductType,

      fetch:function(){
        var self=this;
        var tmpItem;
        //fetch the data using ajax



        $.each(this.productTypesArray, function(i,prodType){

            tmpItem=new ProductType({id:prodType.id, name:prodType.name,    longName:prodType.longName});
            console.log(prodType.name);
            self.add(tmpItem);
        });

        self.trigger("fetchCompleted:ProductsType");

      }
}); 
return ProductsType;

now this doesnt work as it this.productTypesArray is undefined if i console.log it.
(how am I supposed to get this?)

I would have thought I need to go through and add each new ProductType.

the strange bit – if I just have the code

    var ProductsType=Backbone.Collection.extend({

      model:ProductType,

      fetch:function(){
        var self=this;
        var tmpItem;
        //fetch the data using ajax


        self.trigger("fetchCompleted:ProductsType");

      }
});


return ProductsType;

it actually adds the products to the collection? I guess this means I can just pass an array to the collection and do not have to add each productType?

  • 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-15T17:13:01+00:00Added an answer on June 15, 2026 at 5:13 pm

    I guess this means I can just pass an array to the collection and do not have to add each productType?

    Yes, you can pass an array to the collection’s constructor, and it will create the models for you.

    As far as your caching code, it looks like the problem is here:

    if (this.cachedProductTypes !== null) {
        return callback(cachedProductTypes);
    }
    

    The callback statement’s argument is missing this – should be return callback(this.cachedProductTypes).

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

Sidebar

Related Questions

I am trying to store some integers in a file and I am storing
Am trying to get some order information from XML file and trying to store
I am trying to retrieve RSS feed from Yahoo SQL API and storing it
I am trying to read a file line-by-line, and storing that line of data
I am trying to add string values in JsonArray and after that create Json
I'm trying to use SQLXMLBulkLoader4 from C# code into an SQL 2008 DB. But
I was trying to get variable in Query String from URL. But somehow, its
Hi im trying to place a link file in the head from a partial
I'm trying to maintain some Wix code, and am getting the following warning: warning
I'm trying to get a product list for a client from a webservices xml

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.