将模型的 urlRoot 重写为函数失败

Overriding model's urlRoot to a function fails

本文关键字:函数 失败 重写 urlRoot 模型      更新时间:2023-09-26

当我尝试覆盖模型的urlRoot属性并对该模型进行提取时,urlRoot实际上将整个函数作为字符串返回。以下是被覆盖的urlRoot的样子:

urlRoot: function() {
  return "test";
},

它返回以下内容: http://localhost:8000/items/function%20()%20%7B%20%20%20%20%20%20return%20%22test%22;%20%20%20%20%7D

另外,URL 的项目部分来自哪里?

URLRoot 应该是静态字符串

  • 在施工期间定义或

  • 运行时

如果 url 的构造中存在逻辑,则可以将 URl 作为function进行覆盖。

Backbone.Model.extend({
..
urlRoot:"test",
url:function(){
return this.urlRoot + "/" + this.cid;
},
..
}):