如何在 Backbone 中获取单个模型?

发布于 2024-10-18 14:32:19 字数 784 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

背叛残局 2024-10-25 14:32:19

尝试在模型中指定 urlRoot

来自文档:

var Book = Backbone.Model.extend({urlRoot : '/books'});
var solaris = new Book({id: "1083-lem-solaris"});
solaris.fetch();

Try specifying urlRoot in the model:

From the docs:

var Book = Backbone.Model.extend({urlRoot : '/books'});
var solaris = new Book({id: "1083-lem-solaris"});
solaris.fetch();
把回忆走一遍 2024-10-25 14:32:19

你的第二种方法是我用过的方法。尝试将以下内容添加到您的时钟模型中:

url : function() {
  var base = 'clocks';
  if (this.isNew()) return base;
  return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
},

此方法假设您已经在 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:

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.

抚笙 2024-10-25 14:32:19

我个人建议,按照集合中的 Model#url 方法文档

model = new Model(id: 1)
view = new View(model: model) 
collection = new Collection([model])
model.fetch()

记住添加集合 url:

url: "/models"

并在您的视图的初始化函数执行以下操作:

this.model.bind("change", this.render)

这样主干将使用此 url 执行 ajax 请求:

"/models/1"

您的模型将被更新并呈现视图,而无需修改 Collection#url 或 Model#urlRoot

注意:
抱歉,这个示例是在咖啡脚本中出现的,但是您可以轻松地将其转换为 js 添加 var 语句

I personally recommend, following the Model#url method documentation

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:

"/models/1"

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

蓝礼 2024-10-25 14:32:19
var Person = Backbone.Model.extend({urlRoot : '/person/details'});
var myName = new Person({id: "12345"});
myName.fetch();

结果,您发出 Ajax 请求

URL http://[domainName]/person/details/id 

,并收到 JSON 响应。

享受吧!

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.

Enjoiiii !!!

救星 2024-10-25 14:32:19

...如果您不想在模型 urlRoot 上添加尾部斜杠,请执行此操作:

    url : function() {                        
        return this.urlRoot + this.id;
    },

...and do this if you don't want the trailing slash on the model urlRoot:

    url : function() {                        
        return this.urlRoot + this.id;
    },
倾听心声的旋律 2024-10-25 14:32:19

您可能应该通过集合访问对象并始终将其保留在集合中。操作方法如下:

var AllClocks = Backbone.Collection.extend({
  model: Clock,
  url: '/clocks/'
});

var allClocks = new AllClocks();
my_clock = allClocks.add({id: 123});
my_clock.fetch()

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()
青春如此纠结 2024-10-25 14:32:19

我想使用 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());

然后我知道只有在模型中将“idAttribute”设置为“postId”后才能获得正确的网址。
像这样:

var PostModel = Backbone.Model.extend({
    idAttribute: 'postId',
    urlRoot: 'getBlogPost',
    defaults: {
        postTitle: "defaultTitle",
        postTime: "1970-01-01",
        postContent: "defaultContent",
        postAuthor: "anonymous"
    }
});

I want to use RESTful url,but I couldn't understand why 'postId' can't be added to base 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:

var PostModel = Backbone.Model.extend({
    idAttribute: 'postId',
    urlRoot: 'getBlogPost',
    defaults: {
        postTitle: "defaultTitle",
        postTime: "1970-01-01",
        postContent: "defaultContent",
        postAuthor: "anonymous"
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文