由于下划线导致json更改导致的未定义错误

时间:2014-10-15 22:54:15

标签: javascript jquery backbone.js underscore.js marionette

我改变了json结构,所以我得到了未定义的错误...... 你能告诉我为什么我得到这个未定义的错误... 它的工作,但我在另一个地方未定义... 在这个地方this.currentOffers = this.offers;在这个方法setCurrentSports中 提供我的代码小提琴

jsfiddle.net/bb094yto/1

 setCurrentSports: function(offersKey) {
                   // var offers;
debugger;
            if (!offersKey || offersKey === SHOW_ALL) {

                this.offers = _.chain(this.offers).values().flatten().value();
            } else {
                this.offers = this.offers[offersKey];
            }

            this.currentOffersKey = offersKey || SHOW_ALL;
            this.currentOffers = this.offers;

            this.itemsCollection.reset(_.first(this.currentOffers, DEFAULT_LIMIT));
            this.itemsMoreCollection.reset(_.rest(this.currentOffers, DEFAULT_LIMIT));

            this.toggleViewMore();
            this.updateNoResults();
    },


    json

{  
   "sports":{  
      "2":{  
         "soccer":[  
            {  
               "soccerDescription":"uiui",
               "basketballDescription":"uiu",
               "footballDescription":"hhh"
            }
         ]
      },
      "1":{  
         "basketball":[  
            {  
               "soccerDescription":"o",
               "basketballDescription":"i",
               "footballDescription":"i"
            }
         ]
      },
      "6":{  
         "football":[  
            {  
               "soccerDescription":"y",
               "basketballDescription":"h",
               "footballDescription":"ii"
            },
            {  
               "soccerDescription":"o",
               "basketballDescription":"o",
               "footballDescription":"kk"
            }
         ]
      }
   }
}

1 个答案:

答案 0 :(得分:0)

jsons很好,很可能这个。在_.chain(this.offers)中没有设置任何东西。如果将this.offers设置为您的JSON,则_.chain(this.offers).values().flatten().value()有效。