为什么模型关系在backbone.js中是一个挑战

发布于 2024-12-23 01:45:00 字数 623 浏览 0 评论 0原文

我已经看到了有关更改主干中的 toJSON 函数以创建嵌套模型的答案 保存嵌套对象Rails、backbone.js 和accepts_nested_attributes_for

但我试图理解为什么它有效,以及为什么它是任何与仅仅创建不同,

var book = new Book({title: 'my first book', chapters:[{chapter_title: 'first chapter'},{chapter_title: 'second chapter'}...]});
book.url ='books';
book.save();

我一直在尝试使用手动方法保存到 Rails,但我无法保存章节。我觉得这很奇怪,据我所知,有 json 结构,那么建议嵌套属性的“to_JSON”更改有什么作用呢?为什么有必要?

由于骨干模型不需要定义模型属性,那么返回嵌套模型时会出现问题吗?或者它会毫无问题地解析整个 json 吗?

I've seen the answer regarding changing the toJSON function in backbone to create a nested model
Saving nested objects with Rails, backbone.js, and accepts_nested_attributes_for,

But I'm trying to understand why this works, and why it is any different than just creating

var book = new Book({title: 'my first book', chapters:[{chapter_title: 'first chapter'},{chapter_title: 'second chapter'}...]});
book.url ='books';
book.save();

I've been trying to save to rails using the manual method, but i can't save the chapters. I find this strange, as far as I can tell the json structure is there, so what does the 'to_JSON' change recommended for nested attributes do? Why is it necessary?

As backbone models don't need to have model attributes defined, will there be problems when a nested models is returned? Or will it just parse the entire json without issue?

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-12-30 01:45:00

我对 Rails 不太熟悉,但在我看来,重载 toJSON 函数的好处是,在本例中,chapters 可以是模型,也可以是集合或其他东西,并且具有 Backbone.js 的额外功能,而不仅仅是一个 JSON 对象。

I am not very familiar with Rails, but it seems to me that the benefit of overloading the toJSON function is that chapters, in this instance, could be a model, or a collection or something, and have the extra functionality of Backbone.js as opposed to just being a JSON object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文