我在Backbone.js中做了一个小项目,我正在运行一个json-server包来填充它的某些数据。我制作了一个带有数据的db.json文件,并运行了json-server --watch db.json,它启动并在localhost:3000上完美运行。在我的骨干网应用程序,我有这样的:无法从json服务器获取数据
// app/javascripts/collections/resumes.js
define(['backbone', 'models/resume'], function (Backbone, Resume) {
var ResumesCollection = Backbone.Collection.extend({
model: Resume,
url: 'http://localhost:3000/resumes'
});
return ResumesCollection;
});
// app/javascripts/views/resumes/resume.js
define(['backbone', 'jquery'], function (Backbone, $) {
var ResumeView = Backbone.View.extend({
tagName: 'article',
render: function() {
this.$el.html(this.model.get("firstName"));
return this;
}
});
return ResumeView;
});
// app/javascripts/views/resumes/index.js
define(['backbone', 'views/resumes/resume'], function (Backbone, ResumeView) {
var ResumesList = Backbone.View.extend({
tagName: 'section',
initialize: function() {
this.collection.fetch();
},
render: function() {
var resumesView = this.collection.map(function (cv) {
return new ResumeView({model: cv}).render().el;
});
this.$el.html(resumesView);
return this;
}
});
return ResumeList;
});
这是我app/router.js:
define(['backbone',
'collections/resumes',
'views/resumes/resume',
'views/resumes/index'],
function (Backbone, ResumesCollection, ResumeView, ResumeList) {
var AppRouter = Backbone.Router.extend({
routes: {
'resumes': 'showAll'
},
showAll: function() {
this.ResumeList.render();
}
});
return AppRouter;
});
和app/javascripts/main.js我有这样的:
require.config({
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
},
paths: {
jquery: '../node_modules/jquery/dist/jquery',
underscore: '../node_modules/underscore/underscore',
backbone: '../node_modules/backbone/backbone'
}
});
require(['backbone',
'jquery',
'router'
], function (Backbone, $, AppRouter) {
var Router = new AppRouter();
Backbone.history.start({
pushState: true,
root: '/'
});
});
而且我用咕嘟咕嘟运行开发服务器localhost:8080 via gulp-connect和gulp-livereload。但是,当我导航到localhost:8080/resumes时,它会返回我Cannot GET /resumes,但控制台中没有错误。我做错了什么?+