简体   繁体   中英

How to properly set up the Backbone.js urlRoot method

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'
  });

})();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM