gpt4 book ai didi

javascript - Backbone.js - 无法获取数据以进行收集

转载 作者:行者123 更新时间:2023-11-28 07:39:17 26 4
gpt4 key购买 nike

我目前在获取集合数据时遇到问题。它在控制台中给出此错误:

Uncaught TypeError: undefined is not a function

路由器.js:

var App = App || {};

App.Router = Backbone.Router.extend({
routes: {
'': 'homePage'
},

homePage: function() {
var airplanesCollection = new App.Airplanes();
airplanesCollection.fetch().then(function(){
var airplanesView = new App.AirplanesView({ collection: airplanesCollection});
$(".container").html(airplanesView.render().el);
});
}
});

App.router = new App.Router();

集合文件airplanes.js:

var App = App || {};

App.Airplanes = Backbone.Collection.extend({
model: App.Airplane,
url: '/airplanes'
});

模型文件airplane.js:

var App = App || {}

App.Airplane = Backbone.Model.extend({
urlRoot: '/airplanes'
});

如果有人能告诉我哪里出错了,我将非常感激。

非常感谢,

弗雷迪。

最佳答案

我敢打赌 airplanes.jsairplane.js 之前加载/运行。在这种情况下,代码...

App.Airplanes = Backbone.Collection.extend({
model: App.Airplane,
url: '/airplanes'
});

...正在将集合的模型设置为未定义。当尝试在 Collection 上调用 fetch() 时,这会给您带来 TypeError 错误。

短期内,尝试将文件合并为一个文件并确保模型在集合之前定义。从中期来看,看看 RequireJS 来实现真正的模块化 Javascript ——它将在未来为您省去很多麻烦。

关于javascript - Backbone.js - 无法获取数据以进行收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28252265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com