Javascript 如何在 Backbone 中获取单个模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5097107/
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
How do I fetch a single model in Backbone?
提问by James A. Rosen
I have a Clock
model in Backbone:
我Clock
在 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:
我正在尝试从/clocks/123
. 我尝试过的一些事情:
a "class"-level method
“类”级方法
Clock.fetch(123)
// TypeError: Object function (){ ... } has no method 'fetch'
creating an instance and then calling fetch
on it:
创建一个实例然后调用fetch
它:
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):
我尝试创建一个AllClocks
集合资源(即使我在页面上没有使用这样的东西):
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 oneAPI-backed Clock?
我如何只获得一个API 支持的时钟?
采纳答案by Andrew De Andrade
Your second approach is the approach I have used. Try adding the following to your Clock model:
您的第二种方法是我使用的方法。尝试将以下内容添加到您的时钟模型中:
url : function() {
var base = 'clocks';
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
},
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.
这种方法假设您已经在 URL 中实现了带有 hashbang 的控制器,如http://www.mydomain.com/#clocks/123,但即使您还没有,它也应该可以工作。
回答by Hernan S.
回答by makevoid
I personally recommend, following the Model#url method documentation
我个人推荐,遵循Model#url 方法文档
model = new Model(id: 1)
view = new View(model: model)
collection = new Collection([model])
model.fetch()
in your collection remember to add the collection url:
在您的收藏中记得添加收藏网址:
url: "/models"
and in your View's initialize function do:
并在您的视图的初始化函数中执行以下操作:
this.model.bind("change", this.render)
this way backbone will do an ajax request using this url:
这样,主干将使用此 url 执行 ajax 请求:
"/models/1"
your model will be updated and the view rendered, without modifying Collection#url or Model#urlRoot
您的模型将被更新并呈现视图,而无需修改 Collection#url 或 Model#urlRoot
note: sorry this example came out in coffee script, but you can easily translate it to js adding var statements
注意:对不起,这个例子是在咖啡脚本中出现的,但是您可以轻松地将其转换为 js 添加 var 语句
回答by Ankit Garg
var Person = Backbone.Model.extend({urlRoot : '/person/details'});
var myName = new Person({id: "12345"});
myName.fetch();
As a result you make a Ajax request on the
因此,您在
URL http://[domainName]/person/details/id
and you have the JSON response back.
并且您返回了 JSON 响应。
Enjoiiii !!!
享受吧!!!
回答by 4m1r
...and do this if you don't want the trailing slash on the model urlRoot:
...如果您不想在模型 urlRoot 上使用尾部斜杠,请执行此操作:
url : function() {
return this.urlRoot + this.id;
},
回答by ssobczak
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:
您可能应该通过集合访问对象并始终将其保存在集合中。这是如何做到的:
var AllClocks = Backbone.Collection.extend({
model: Clock,
url: '/clocks/'
});
var allClocks = new AllClocks();
my_clock = allClocks.add({id: 123});
my_clock.fetch()
回答by Araell
I want to use RESTful url,but I couldn't understand why 'postId' can't be added to base url.
我想使用 RESTful url,但我不明白为什么不能将“postId”添加到基本 url。
var PostModel = Backbone.Model.extend({
urlRoot: 'getBlogPost',
defaults: {
postTitle: "defaultTitle",
postTime: "1970-01-01",
postContent: "defaultContent",
postAuthor: "anonymous"
}
});
var post = new PostModel({
postId: 1
});
alert(post.url());
Then I know only after I set 'idAttribute' as 'postId' in Model can I get the right url. like this:
然后我知道只有在模型中将 'idAttribute' 设置为 'postId' 后,我才能获得正确的 url。像这样:
var PostModel = Backbone.Model.extend({
idAttribute: 'postId',
urlRoot: 'getBlogPost',
defaults: {
postTitle: "defaultTitle",
postTime: "1970-01-01",
postContent: "defaultContent",
postAuthor: "anonymous"
}
});