如何在 Backbone 中获取单个模型?
我在 Backbone 中有一个 Clock
模型:
var Clock = Backbone.Model.extend({});
我正在尝试获取一个包含来自 /clocks/123
的最新信息的实例。我尝试过的一些事情:
创建一个实例的“类”级方法
Clock.fetch(123)
// TypeError: Object function (){ ... } has no method 'fetch'
,然后在其上调用fetch
:
c = new Clock({id: 123})
c.fetch()
// Error: A 'url' property or function must be specified
一个集合
我尝试创建一个AllClocks
集合资源(即使我在页面上没有使用这样的东西):
var AllClocks = Backbone.Collection.extend({
model: Clock,
url: '/clocks/'
});
var allClocks = new AllClocks();
allClocks.fetch(123);
// returns everything from /clocks/
我如何获得 一个 API 支持的时钟?
I have a Clock
model in Backbone:
var Clock = Backbone.Model.extend({});
I'm trying to get an instance of that that has the latest information from /clocks/123
. Some things I've tried:
a "class"-level method
Clock.fetch(123)
// TypeError: Object function (){ ... } has no method 'fetch'
creating an instance and then calling fetch
on it:
c = new Clock({id: 123})
c.fetch()
// Error: A 'url' property or function must be specified
a collection
I tried creating an AllClocks
collection resource (even though I have no use for such a thing on the page):
var AllClocks = Backbone.Collection.extend({
model: Clock,
url: '/clocks/'
});
var allClocks = new AllClocks();
allClocks.fetch(123);
// returns everything from /clocks/
How do I just get one API-backed Clock?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试在模型中指定 urlRoot:
来自文档:
Try specifying urlRoot in the model:
From the docs:
你的第二种方法是我用过的方法。尝试将以下内容添加到您的时钟模型中:
此方法假设您已经在 URL 中实现了带有 hashbang 的控制器,如下所示 http://www.mydomain.com/#clocks/123 ,但即使您还没有,它也应该可以工作。
Your second approach is the approach I have used. Try adding the following to your Clock model:
This approach assumes that you have implemented controllers with the hashbang in your URL like so, http://www.mydomain.com/#clocks/123 , but it should work even if you haven't yet.
我个人建议,按照集合中的 Model#url 方法文档,
记住添加集合 url:
并在您的视图的初始化函数执行以下操作:
这样主干将使用此 url 执行 ajax 请求:
您的模型将被更新并呈现视图,而无需修改 Collection#url 或 Model#urlRoot
注意:
抱歉,这个示例是在咖啡脚本中出现的,但是您可以轻松地将其转换为 js 添加 var 语句
I personally recommend, following the Model#url method documentation
in your collection remember to add the collection url:
and in your View's initialize function do:
this way backbone will do an ajax request using this url:
your model will be updated and the view rendered, without modifying Collection#url or Model#urlRoot
note:
sorry this example came out in coffee script, but you can easily translate it to js adding var statements
结果,您发出 Ajax 请求
,并收到 JSON 响应。
享受吧!
As a result you make a Ajax request on the
and you have the JSON response back.
Enjoiiii !!!
...如果您不想在模型 urlRoot 上添加尾部斜杠,请执行此操作:
...and do this if you don't want the trailing slash on the model urlRoot:
您可能应该通过集合访问对象并始终将其保留在集合中。操作方法如下:
You probably should be accessing the object trough a collection and keeping it in the collection all the time. This is how to do it:
我想使用 RESTful url,但我不明白为什么不能将“postId”添加到基本 url。
然后我知道只有在模型中将“idAttribute”设置为“postId”后才能获得正确的网址。
像这样:
I want to use RESTful url,but I couldn't understand why 'postId' can't be added to base url.
Then I know only after I set 'idAttribute' as 'postId' in Model can I get the right url.
like this: