Backbone.js fetch() JSON to model get() returns undefined

前端 未结 2 814
囚心锁ツ
囚心锁ツ 2021-01-23 18:51

Basically I want to fetch a JSON file and store it in a model. However, when I try to access the attributes via get() it returns undefined. So lets

2条回答
  •  -上瘾入骨i
    2021-01-23 19:01

    I don't know if it's a typo or not but you are not passing model to the callback

    instplayer.on('change', function(){
      console.log(model);
      console.log(model.get(games));
    })
    

    it should be

    instplayer.on('change', function(model, options){
      console.log(model);
      console.log(model.get("games"));
    })
    

    then games must be a string not a variable.

    Another thing I suggest you to be aware of is what the json returns; sometimes you have to override the parse function to get the exact results.

    Again if your fetching an array and not a single object, maybe you should use a collection of players

提交回复
热议问题