我正在构建一个简单的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?
更多推荐
发布评论