Javascript 必须在 Backbone.js 中指定“url”属性或函数错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6030677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:57:12  来源:igfitidea点击:

A 'url' property or function must be specified error in Backbone.js

javascriptjqueryhtmlbackbone.js

提问by Sergei Basharov

I am trying to make a small app to learn how Backbone works. I took example app from source called Todo. I have created my app from scratch using snippets from Todo app. I think these apps look very similar but for some reason I can't make work some things that work fine in the example app. I get an error:

我正在尝试制作一个小应用程序来了解 Backbone 的工作原理。我从名为Todo 的源中获取了示例应用程序。我使用 Todo 应用程序中的片段从头开始创建了我的应用程序。我认为这些应用程序看起来非常相似,但出于某种原因,我无法使示例应用程序中的某些功能正常工作。我收到一个错误:

A 'url' property or function must be specified

The other problem is that I can't make this code from the example work:

另一个问题是我无法使示例中的此代码工作:

this.model.bind('change', this.render);

It says there is no such a function as bind. I checked all libraries versions and code and can't realize what I do wrong. What can I do about this?

它说没有绑定这样的功能。我检查了所有库版本和代码,但无法意识到我做错了什么。我该怎么办?

回答by Julien

The TODO example is relying on localStorage thus it does not define a url (as it is local). However, when you use the default Backbone.sync implementation, you need to define a url attribute on your collections and models (it can be either static or a function). Not doing so results in the error you got.

TODO 示例依赖于 localStorage,因此它没有定义 url(因为它是本地的)。然而,当你使用默认的 Backbone.sync 实现时,你需要在你的集合和模型上定义一个 url 属性(它可以是静态的或函数)。不这样做会导致您得到错误。

As for the this.model.bind, I guess you lost the reference to your model somehow. Two things: thisis not what you think it is or this.modelis not defined. Post more code to have complete answers.

至于this.model.bind,我猜你不知何故丢失了对你的模型的引用。两件事:不是你认为的那样或者this.model没有定义。发布更多代码以获得完整答案。

回答by Rimian

The collection attempts to load a bunch of models from json output at the URL:

该集合尝试从 URL 处的 json 输出加载一堆模型:

window.MyList = Backbone.Collection.extend({
  model: MyModel,
  url: 'someurl.json', // load a bunch of json objects into models.
});

If that URL points to a json output of your models, you're good to go.

如果该 URL 指向模型的 json 输出,那么您就可以开始了。

You can also override the way a collection makes restful call back to your server to support legacy servers or a local storage adapter: http://documentcloud.github.com/backbone/#Sync

您还可以覆盖集合对您的服务器进行宁静回调以支持旧服务器或本地存储适配器的方式:http: //documentcloud.github.com/backbone/#Sync

回答by sairfan

Assigning to collection property url worked for me

分配给集合属性 url 对我有用

todoList.url = "/GetData/GetTodo";