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

发布于 2024-11-07 22:12:45 字数 479 浏览 0 评论 0原文

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

A 'url' property or function must be specified

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

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

它说不存在像bind这样的函数。我检查了所有库版本和代码,但无法意识到我做错了什么。对此我能做什么?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼泪 2024-11-14 22:12:45

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

至于 this.model.bind,我猜您以某种方式丢失了对模型的引用。有两件事:this 不是您想象的那样,或者 this.model 未定义。发布更多代码以获得完整的答案。

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.

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

失退 2024-11-14 22:12:45

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

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

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

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

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

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.

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

栀梦 2024-11-14 22:12:45

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

todoList.url = "/GetData/GetTodo";

Assigning to collection property url worked for me

todoList.url = "/GetData/GetTodo";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文