For my current project in Backbone.js, I'm utilizing the json-server
package to populate it with data. I've created a db.json
file containing the data and executed the command json-server --watch db.json
. The server started successfully and is running on localhost:3000
. In my Backbone application, the following code snippets are in place:
// 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;
});
This is my 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;
});
In app/javascripts/main.js
, the configuration is as follows:
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: '/'
});
});
Additionally, I am using Gulp to run a development server on localhost:8080
using gulp-connect
and gulp-livereload
. However, when I try to access localhost:8080/resumes
, I receive a Cannot GET /resumes
message, despite no errors appearing in the console. What could be causing this issue?