में एक स्थानीय JSON फ़ाइल से एक रीढ़ संग्रह में कुछ डेटा लोड करने के लिए यह बहुत ही बुनियादी कोड का उपयोग कर कोशिश कर रहा हूँ:JSON फ़ाइल से बैकबोन संग्रह में डेटा लोड करें?
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.fetch({ url: "/init.json"});
console.log('AllStudents', AllStudents);
सांत्वना बयान में AllStudents
खाली है। लेकिन init.json
निश्चित रूप से लोड किया जा रहा है। ऐसा लगता है:
[
{ text: "Amy", grade: 5 },
{ text: "Angeline", grade: 26 },
{ text: "Anna", grade: 55 }
]
मैं क्या गलत कर रहा हूं?
अद्यतन: मैं भी .fetch()
कॉल के ऊपर एक reset
श्रोता जोड़ने की कोशिश की है, लेकिन वह फायरिंग नहीं कर रहा है या तो:
AllStudents.bind("reset", function() {
alert('hello world');
});
AllStudents.fetch({ url: "/init.json"});
कोई चेतावनी दिखाई देता है।
अद्यतन 2:
$(function(){
window.Student = Backbone.Model.extend({
});
window.Students = Backbone.Collection.extend({
model: Student,
});
window.AllStudents = new Students();
AllStudents.url = "/init.json";
AllStudents.bind('reset', function() {
console.log('hello world');
});
AllStudents.fetch();
AllStudents.fetch({ url: "/init.json", success: function() {
console.log(AllStudents);
}});
AllStudents.fetch({ url: "/init.json" }).complete(function() {
console.log(AllStudents);
});
});
केवल एक सांत्वना बयान भी तीसरे fetch()
कॉल में प्रकट होता है, है, और यह एक खाली वस्तु है: इस स्क्रिप्ट (पूरा यहाँ reproduced) की कोशिश कर रहा।
अब मैं बिल्कुल परेशान हूं। मैं क्या गलत कर रहा हूं?
JSON फ़ाइल को एप्लिकेशन/जेसन के रूप में सेवा दी जा रही है, इसलिए इसका कोई लेना-देना नहीं है।
यह एक अच्छा सवाल है। आपको अपने JSON –