से बैकबोन पॉप्युलेट संग्रह संग्रह नीचे मेरे द्वारा मैन्युअल रूप से बनाए गए संग्रह के लिए वर्तमान कोड संरचना है। मेरे पास मेरे सर्वर पर एक जेसन फ़ाइल है जिसे मैं अब लोड करने की कोशिश कर रहा हूं और मूल रूप से मैन्युअल को हटा देता हूं और उस डेटा के आधार पर संग्रह बनाता हूं। यह सोच रहा था कि इसे समायोजित करने में सहायता के लिए मुझे अपने कोड में नीचे क्या बदलना होगा।बाहरी जेसन
var Game = Backbone.Model.extend({
defaults: {
name: 'John Doe',
age: 30,
occupation: 'worker'
}
});
var GameCollection = Backbone.Collection.extend({
model: Game,
url: 'path/to/json',
parse: function(response) {
return response;
}
});
var GamesView = Backbone.View.extend({
tagName: 'ul',
render: function() {
//filter through all items in a collection
this.collection.each(function(game){
var gameView = new GameView({model: game});
this.$el.append(gameView.render().el);
}, this)
return this;
}
});
var GameView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#gameTemplate').html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var gameCollection = new GameCollection([
{
name: 'John Doe',
age: 30,
occupation: 'worker'
},
{
name: 'John Doe',
age: 30,
occupation: 'worker'
},
{
name: 'John Doe',
age: 30,
occupation: 'worker'
}
]);
var gamesView = new GamesView({collection: gameCollection});
$(document.body).append(gamesView.render().el);
तो आप वर्तमान में 'नया गेम चयन (some_big_array_of_stuff)' कह रहे हैं, लेकिन अब आप सर्वर से संग्रह लोड करना चाहते हैं? –
हां, मैं परीक्षण के लिए बनाए गए ऑब्जेक्ट्स के मैन्युअल सरणी का उपयोग नहीं करना चाहता हूं, लेकिन अब मैं सर्वर पर एक जेसन ऑब्जेक्ट में लोड करने का प्रयास करना चाहता हूं और उस डेटा को पार्स करने का प्रयास करना चाहता हूं। सुनिश्चित नहीं है कि इसके बारे में कैसे जाना है। – Anks