尝试从集合中获取特定模型(trying to get a specific model out of a collection)

编程入门 行业动态 更新时间:2024-10-28 12:18:53
尝试从集合中获取特定模型(trying to get a specific model out of a collection)

我正在构建一个简单的backbone.js应用程序,而不使用“真正的数据库”。我的数据库非常简单,看起来像这样:

[ { "Name": "Daniel", "Month": "abc", "year": "83" }, { "Name": "Johan", "Month": "abg", "year": "33" } ]

我成功将数据提取到集合实例中,我做了console.log并看到它有效:

this.nameList = new NameList(); this.nameList.fetch(); console.log(this.nameList);

在这里,我很好,现在我试图只使用集合中的一个模型,没有任何作用。 我试过了:

console.log(this.nameList.at(1)); console.log(this.nameList.get(id)); //when id is passed as a number. console.log(this.nameList.getByCid(id)) //when id is passed as a number.

我总是得到“未定义”。

我如何使用这样的简单数据库? 我在哪里可以阅读这些东西?

作为更新,它在使用集合时适用于我:

this.projectList = new ProjectList; this.projectList.fetch({ success: function (data) { console.log(data.get(id)); } });

使用单个模型时不起作用:

this.project = new Project({id: id}); this.project.fetch({ success: function (data) { console.log(data); } });

现在我得到:GET ........ / Client / web / Data / projects / 3 404(Not Found)

为什么?

I"m building a simple backbone.js app, without using a "real database". My data base is very simple and looks something like that:

[ { "Name": "Daniel", "Month": "abc", "year": "83" }, { "Name": "Johan", "Month": "abg", "year": "33" } ]

Iv'e succeeded fetching the data into a collection instance, I did console.log and saw it worked:

this.nameList = new NameList(); this.nameList.fetch(); console.log(this.nameList);

Up here I'm fine, now I tried to use only one of the models in the collection and nothing works. I tried:

console.log(this.nameList.at(1)); console.log(this.nameList.get(id)); //when id is passed as a number. console.log(this.nameList.getByCid(id)) //when id is passed as a number.

I always get "undefined".

How can I work with a simple database like that? Where can I read about this stuff?

As an update, it works for me when working with a collection:

this.projectList = new ProjectList; this.projectList.fetch({ success: function (data) { console.log(data.get(id)); } });

Not working when working with a single model:

this.project = new Project({id: id}); this.project.fetch({ success: function (data) { console.log(data); } });

now I get : GET ......../Client/web/Data/projects/3 404 (Not Found)

Why?

最满意答案

/Client/web/Data/projects/3 404 (Not Found)

你确定你真的在RESTian位置提供服务吗?

集合的提取默认行为是在集合URL [/ Client / web / Data / projects]上执行GET,您说这是提供正确的数据。

但是当你在单个模型上调用fetch()时,它会将该模型的id附加到集合URL:/ Client / web / Data / projects / 3,它返回404 / Not Found响应。 如果您手动尝试该URL,您实际上是否得到了回复?

至于为什么console.log(this.nameList.get(id))返回undefined,似乎没有正确填充nameList。 看一下console.log(this.nameList.models)的输出 - 它实际上是否有您期望的模型?

/Client/web/Data/projects/3 404 (Not Found)

Are you sure you're actually serving something at that RESTian location?

The collection's default behavior for a fetch will be to perform a GET on the collection URL [/Client/web/Data/projects] which you say is serving the correct data.

But when you call fetch() on an individual model it appends that model's id to the collection URL: /Client/web/Data/projects/3 which is returning a 404/Not Found response. If you try that URL manually do you actually get a response?

As for why console.log(this.nameList.get(id)) is returning undefined, it seems nameList didn't get populated correctly. Take a look at the output of console.log(this.nameList.models) - does it actually have the models you were expecting?

更多推荐

本文发布于:2023-08-08 00:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   specific   collection   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!