Backbone JS 处理保存模型?

发布于 2024-11-04 15:02:30 字数 596 浏览 1 评论 0原文

使用 Backbone,我可以获取如下所示的集合,并为每条记录呈现一个 Backbone 视图:

Person = Backbone.Model.extend({});

var PersonCollection = Backbone.Collection.extend({

    model: Person,
    url: '/home/people/'
});

当我像下面这样使用 Backbone 启动一个新人时,它不是应该已经处理了 .save()通过发布到上述集合中定义的 URL 来实现功能吗?

var p = new Person({ Name: 'Andrew', Age: 24 });
p.save();
// Uncaught Error: A 'url' property or function must be specified
// I thought it was supposed to use the Collection's URL?
// I can get around this by explicitly setting p.URL but doesn't seem right

Using Backbone I can fetch the Collection like below and render a Backbone View for each record:

Person = Backbone.Model.extend({});

var PersonCollection = Backbone.Collection.extend({

    model: Person,
    url: '/home/people/'
});

When I spin up a new person using Backbone like below, isn't it supposed to already handle the .save() functionality by posting to the URL defined in the above collection?

var p = new Person({ Name: 'Andrew', Age: 24 });
p.save();
// Uncaught Error: A 'url' property or function must be specified
// I thought it was supposed to use the Collection's URL?
// I can get around this by explicitly setting p.URL but doesn't seem right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦冥 2024-11-11 15:02:30

看起来一种方法是使用当前集合和 create 来处理将其添加到集合中并保存它

this.people.create(p);

源如下。 Create 不在集合中,因此它将 Models 集合设置为 this 然后,一旦 Save it 成功,它将 Model 添加到您的集合中。因此它将模型传递给 getUrl,getUrl 查看模型上的 URL 属性,模型又将集合再次传递给 getUrl...这样我们就不必重新定义 URL...

create : function(model, options) {
  var coll = this;
  options || (options = {});
  if (!(model instanceof Backbone.Model)) {
    model = new this.model(model, {collection: coll});
  } else {
    model.collection = coll;
  }
  var success = function(nextModel, resp) {
    coll.add(nextModel);
    if (options.success) options.success(nextModel, resp);
  };
  return model.save(null, {success : success, error : options.error});
},

Looks like an approach is to use the current collection and create which handles adding it to the collection and saving it

this.people.create(p);

The source is below. Create is off the collection, so it sets the Models collection to this Then once the Save it success it adds Model to your collection. So it passes the model to getUrl, the getUrl looks at the URL property on the model, which in turn passes the collection to getUrl again... that way we don't have to redefine URL...

create : function(model, options) {
  var coll = this;
  options || (options = {});
  if (!(model instanceof Backbone.Model)) {
    model = new this.model(model, {collection: coll});
  } else {
    model.collection = coll;
  }
  var success = function(nextModel, resp) {
    coll.add(nextModel);
    if (options.success) options.success(nextModel, resp);
  };
  return model.save(null, {success : success, error : options.error});
},
余罪 2024-11-11 15:02:30

您只需设置集合

Person.collection = PersonCollection

现在它就会神奇地知道 url。

You can just set the collection

Person.collection = PersonCollection

Now it will magically know the url.

我不咬妳我踢妳 2024-11-11 15:02:30

根据Backbone.sync中的backbone.js代码,情况似乎并非如此。也许文档是旧的或不正确的。代码如下:

// Helper function to get a URL from a Model or Collection as a property
// or as a function.
var getUrl = function(object) {
  if (!(object && object.url)) throw new Error("A 'url' property or function must be specified");
  return _.isFunction(object.url) ? object.url() : object.url;
};

这向我表明该模型需要自己的 url。所以你应该这样做:

Person = Backbone.Model.extend({
  url: function() {
    if (this.isNew()) return '/home/people/';
    return '/home/whatever-this-route-would-be';
  }
});

According to the backbone.js code in Backbone.sync, this does not seem like the case. Perhaps the documentation is old or not correct. The code reads:

// Helper function to get a URL from a Model or Collection as a property
// or as a function.
var getUrl = function(object) {
  if (!(object && object.url)) throw new Error("A 'url' property or function must be specified");
  return _.isFunction(object.url) ? object.url() : object.url;
};

which suggests to me that the model needs its own url. So you should just do this:

Person = Backbone.Model.extend({
  url: function() {
    if (this.isNew()) return '/home/people/';
    return '/home/whatever-this-route-would-be';
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文