简体   繁体   English

如何正确设置Backbone.js urlRoot方法

[英]How to properly set up the Backbone.js urlRoot method

I am having trouble understanding how urlRoot works in Backbone.js. 我无法理解urlRoot在Backbone.js中的工作方式。 I am attempting to fetch a task with a specific _id from a NodeJS/MongoDB backend. 我正在尝试从NodeJS / MongoDB后端获取具有特定_id的任务。

What I'm not exactly clear on is how to pass the id attribute into the URI. 我不清楚的是如何将id属性传递到URI中。 I've been doing the following from Chrome dev tools: 我一直在使用Chrome开发者工具执行以下操作:

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". 查看http://backbonetutorials.com/what-is-a-model/以获得指导,我会说您不需要定义idAttribute,而urlRoot应该只是“ / tasks”。 Backbone should handle the rest. 骨干应该处理其余的事情。

Note: I haven't used Backbone myself yet but have been reading up on it. 注意:我自己还没有使用过Backbone,但一直在阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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