首页 文章

Backbone.js fetch()JSON到模型get()返回undefined

提问于
浏览
1

基本上我想获取 JSON 文件并将其存储在模型中 . 但是,当我尝试通过 get() 访问属性时,它返回undefined . 所以假设JSON文件有一个由具有某些属性的对象组成的数组游戏 . 它没有做这样的事情 .

var player = Backbone.Model.extend({
   initialize: function(app, options) {
      this.app = app;
      var _this = this;

      this.fetch({
         url: "someurl",
         success: function() {
            console.log("success");
         }
      });
   }
});

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

所以我想我需要一个事件来确保在数据真正可用时调用 get() . 但这仍然是未定义的 . 我需要做什么?

2 回答

  • 1

    所以我想象你有一个json为你的播放器这样(我已经嘲笑它here为下面的例子工作):

    {
        "username": "joe",
        "games": [
            {
                "title": "Pacman"
            }, {
                "title": "Super Mario" } 
        ]
    }
    

    这是一个完整的工作示例,说明如何处理这种数据的管理和渲染:

    var Game = Backbone.Model.extend({
      defaults: {
        title: ''
      }
    });
    
    var Games = Backbone.Collection.extend({
      model: Game
    });
    
    var Player = Backbone.Model.extend({
      defaults: {
        username: ''
      },
      url: 'http://www.mocky.io/v2/56261127250000a01accb34f',
      initialize: function(){
        this.games = new Games();
        this.listenTo( this, "sync", this.initGames );
        this.fetch();
      },
      initGames: function(){
        this.games.add( this.get('games') );
        this.trigger('ready', this);
      }
    });
    
    var PlayerView = Backbone.View.extend({
      template: _.template('<h1><%=username%></h1> and his games: <ol class="games"></ol>'),
      render: function(){
        this.$el.html( this.template( this.model.toJSON() ) );
        this.model.games.each( this.renderGame, this );
        return this;
      },
      renderGame: function(game, i){
        var gameView = new GameView({ model: game });
        gameView.render().$el.appendTo( this.$('.games') );
      }
    });
    
    var GameView = Backbone.View.extend({
      tagName: 'li',
      template: _.template('<strong>Game:</strong> <%=title%>'),
      render: function(){
        this.$el.html( this.template( this.model.toJSON() ));
        return this;
      }
    });
    
    
    var dude = new Player();
    dude.on('ready', function(player){
      var playerView = new PlayerView({ model: player });
      playerView.render().$el.appendTo( document.body );
    });
    
    <script src='http://code.jquery.com/jquery.js'></script>
    <script src='http://underscorejs.org/underscore.js'></script>
    <script src='http://backbonejs.org/backbone.js'></script>
    
  • 0

    我不是't know if it'是一个错字,但你没有将 model 传递给 callback

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

    它应该是

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

    那么 games 必须是 string 而不是 variable .

    我建议你注意的另一件事是 json 返回的内容;有时您必须覆盖 parse 函数才能获得准确的结果 .

    再次,如果您提取 array 而不是单个对象,也许您应该使用 collection players

相关问题