为什么模型关系在backbone.js中是一个挑战
我已经看到了有关更改主干中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 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 thatchapters
, 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.