I am having trouble understanding how urlRoot
works in Backbone.js. I am attempting to fetch a task with a specific _id
from a NodeJS/MongoDB backend.
What I'm not exactly clear on is how to pass the id attribute into the URI. I've been doing the following from Chrome dev tools:
var task = new App.Models.Task({ id: '51c09ae7d3b35d29d4dfdecd' });
task.fetch();
And I receive the following error:
GET http://localhost:3000/tasks/:_id 500 (Internal Server Error)
How do I properly set this up?
Here's my Backbone code:
(function() {
window.App = {
Models: {},
Views: {},
Collections: {}
};
App.Models.Task = Backbone.Model.extend({
defaults: {
title: '',
completed: false
},
idAttribute: "_id",
urlRoot: '/tasks/:_id'
});
})();
BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.
Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
Looking at http://backbonetutorials.com/what-is-a-model/ for guidance I would say you don't need to define the idAttribute and your urlRoot should just be "/tasks". Backbone should handle the rest.
Note: I haven't used Backbone myself yet but have been reading up on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With