我正在尝试从节点服务器获取一些数据,这可以正常工作,但是当我尝试通过Backbone fetch(或同步)获取数据时,请求失败。我注意到,由于某种原因,实际请求是错误的:'GET http://localhost:3000/socket.io/1/'应该是'GET http://localhost:3000/cars',因为'/ cars'是Backbone使用的URL字段的值这些行动的惯例。这些是相关模块:
var Backbone = require("backbone");
var Car = require('models/car');
var Cars = Backbone.Collection.extend ({
model: Car,
url: '/cars',
// Unselect all Car Cards
resetSelected: function() {
for (var i=1; i<=this.length; ++i) {
var carcard=this.get(i);
carcard.set({"selected": false});
console.log(carcard.attributes.name + ' unselected');
}
},
// Select a specific model from the collection
selectByID: function(id) {
this.resetSelected();
var carcard = this.get(id);
carcard.set({"selected": true});
console.log(carcard.attributes.name + " selected");
return carcard.attributes.id;
}
});
module.exports = Cars;
一个模特:
var Backbone = require("backbone");
var Car = Backbone.Model.extend({
defaults: {
year: 2011,
brand: "Brand",
model: "Model",
name: "Car Name",
pictureFle: "img/car.jpg",
kmTraveled: 0,
litresSpent: 0,
selected: false
},
});
module.exports = Car;
我试图像这样填充这个集合:
var cars = new Cars();
cars.fetch();
但是,正如我解释的那样,失败了。任何想法可能是什么问题?