2015-10-20 76 views
1

基本上我想獲取JSON文件並將其存儲在模型中。但是,當我嘗試通過get()訪問屬性時,它返回undefined。所以可以說JSON文件有一個由具有一些屬性的對象組成的數組遊戲。它並不重要。只是想將它們保存在模型中並訪問它們。所以我正在做這樣的事情。Backbone.js fetch()JSON模型get()迴歸undefined

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()被調用。但是這仍然返回undefined。我需要做什麼?謝謝。

+0

你的意思是你取的球員,而且也多維數據,包含遊戲嗎?是這樣的: '{username:「joe」,games:[{title:「Game One」},{title:「Game Two」}}}' 然後你只想訪問'games'屬性, 是對的嗎? – Yura

+0

是的,這是正確的。只要我能從JSON數據中獲取任何東西。我的意思是當我通過console.log查看對象時看到數據,但get()只是簡單地返回undefined。當然,我可以毫無問題地返回帶有默認選項的模型的一些值。 – riasc

+0

在確定你將'.get'方法傳遞給''model.get(「games」)'的引號後,我還建議使用'sync'事件而不是'change'。當內容來自服務器時,'sync'將確保觸發。 「change」會觸發對該模型數據的任何更改,這可能比您想要的更頻繁。 – Yura

回答

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

太棒了。非常感激。這正是我需要的。漂亮的天才,將一個集合添加到模型。不知道這甚至是可能的。謝謝。 – riasc

0

我不知道這是否是一個錯字或無噸,但你不及格modelcallback

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,而不是一個單一的對象,也許你應該使用collectionplayers

+0

感謝您的回答。是的,這確實是一個錯字。除了這段代碼之外,你的建議應該讓我回到陣列嗎?我嘗試了一個集合,然後簡單地做了'parse:function(response){return response.games; }'這也填充集合,但我不知何故無法訪問填充的屬性。 – riasc

+0

你給'collection'分配了一個'model'嗎? – steo

+0

感謝您的提示。 – riasc