Javascript 从服务器获取单个 Backbone 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7382243/
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
Fetching a single Backbone model from server
提问by cjroebuck
Say I have a route setup:
假设我有一个路线设置:
'photos/:id' : 'showPhoto'
and somebody shares the url: www.mysite.com/photos/12345
with a friend.
有人www.mysite.com/photos/12345
与朋友分享了网址:
When their friend clicks on the shared link, showPhoto
gets called back with 12345 passed as the id. I cant figure out how to fetch the model from the server, because even when setting its id property and calling fetch(), backbone thinks that the model isNew
and so the ajax request url is just /photos
instead of /photos/12345
:
当他们的朋友点击共享链接时,showPhoto
会以 12345 作为 id 传递回来。我无法弄清楚如何从服务器获取模型,因为即使在设置其 id 属性并调用 fetch() 时,主干也认为模型isNew
等 ajax 请求 url 只是/photos
而不是/photos/12345
:
showPhoto: (id) ->
photo = new models.Photo _id:id
photo.fetch #does a GET to /photos, I would have expected it to request /photos/12345
success: () ->
render photo view etc...
Photo = Backbone.Model.extend
idAttribute: '_id'
urlRoot: '/photos'
The model Photo
is usually part of a collection, but in this scenario someone is visiting the site directly and only expects to see data for one photo, so the collection is not instantiated in this state of the app.
模型Photo
通常是集合的一部分,但在这种情况下,有人直接访问站点并且只希望看到一张照片的数据,因此在应用程序的这种状态下不会实例化集合。
Is the solution to load the entire collection of photos and then use collection.getById(id)
? This just seems way too inefficient when I just want to load the properties for one model.
解决方案是加载整个照片集然后使用collection.getById(id)
吗?当我只想加载一个模型的属性时,这似乎太低效了。
回答by Derick Bailey
if you don't have the model as part of a collection, you have to tell the model the full url manually. it won't auto-append the id to the urlRoot that you've specified. you can specify a function as the urlRoot to do this:
如果您没有将模型作为集合的一部分,则必须手动告诉模型完整的 url。它不会将 id 自动附加到您指定的 urlRoot。您可以指定一个函数作为 urlRoot 来执行此操作:
Photo = Backbone.Model.extend({
urlRoot: function(){
if (this.isNew()){
return "/photos";
} else {
return "/photos/" + this.id;
}
}
});
Backbone uses the id
of the model to determine if it's new or not, so once you set that, this code should work correctly. if it doesn't, you could always check for an id in the if-statement instead of checking isNew.
Backbone 使用id
模型的 来确定它是否是新的,所以一旦你设置了它,这段代码应该可以正常工作。如果没有,您总是可以在 if 语句中检查 id,而不是检查 isNew。
回答by Alan
You do not need to tell backbone whether or not to append the id the url. Per the documentation: http://backbonejs.org/#Model-fetch, you may simply set the urlRoot to the equivalent of the url in a collection.
您不需要告诉主干是否将 id 附加到 url。根据文档:http: //backbonejs.org/#Model-fetch ,您可以简单地将 urlRoot 设置为集合中的 url 等价物。
Backbone will automatically append the desired id to the url, provided you use one of the following methods:
如果您使用以下方法之一,Backbone 将自动将所需的 id 附加到 url:
model.set("id", 5); //After initialized
model = new Backbone.Model({id: 5}); //New model
If you manually set the id in the attributes hash or directly on the model, backbone won't be aware of it.
如果您在属性散列中或直接在模型上手动设置 id,主干将不会意识到它。
model.id = 5; //Don't do this!
回答by makevoid
there's already a similar question: "How do I fetch a single model in Backbone?"
已经有一个类似的问题:“如何在 Backbone 中获取单个模型?”
my answer there should work for you (and it's in coffeescript)
我的答案应该对你有用(它是在咖啡脚本中)
also remember to check Backbone Model#url documentation, it's all explained there
还记得检查Backbone Model#url 文档,那里都有解释
回答by timDunham
I would bootstrap the collection (by rendering the following to the page) with just one model in it like this:
我将引导集合(通过将以下内容渲染到页面),其中只有一个模型,如下所示:
photos = new PhotoCollection();
photos.reset([ @Html.ToJson(Model) ]);
Note that the server side code that I use is ASP.Net MVC so use something specific to your server side architecture. Also note that the square brackets are important as they take your singular model and wrap it in an array.
请注意,我使用的服务器端代码是 ASP.Net MVC,因此请使用特定于您的服务器端架构的代码。另请注意,方括号很重要,因为它们采用您的单一模型并将其包装在数组中。
Hope that's helpful.
希望这有帮助。