保存 Backbone.js 模型时堆栈溢出
我有一个简单的 Backbone.js 模型:
class Avia.Student extends Backbone.Model
告诉过你这很简单 :-) 无论如何,我将其保存在以下视图中(为了清晰起见进行了剪裁):
class Avia.StudentView extends Backbone.View
render: =>
html = JST['views/student_view_template'](model: @model)
@el.html(html)
Backbone.ModelBinding.bind(@)
$('#save').bind('click', @save)
save: (e) =>
e.preventDefault()
@model.save(
success: =>,
error: =>
)
当我单击保存按钮时,调用 @save() ,但失败出现以下错误(再次,为了清楚起见,进行了剪断,因为显然它持续了很长一段时间):
Uncaught RangeError: Maximum call stack size exceeded
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
有人可以告诉我我做错了什么吗?我不明白为什么会发生这种情况......
I have a simple Backbone.js model:
class Avia.Student extends Backbone.Model
Told you it was simple :-) Anyhow, I'm saving it in the following view (snipped for clarity):
class Avia.StudentView extends Backbone.View
render: =>
html = JST['views/student_view_template'](model: @model)
@el.html(html)
Backbone.ModelBinding.bind(@)
$('#save').bind('click', @save)
save: (e) =>
e.preventDefault()
@model.save(
success: =>,
error: =>
)
When I click the save button, @save() is called, but fails with the following error (again, snipped for clarity as obviously it continues for a long time):
Uncaught RangeError: Maximum call stack size exceeded
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
_.extend._onModelEvent:635
Backbone.Events.trigger:117
Could someone please tell me what I'm doing wrong? I don't understand why this is happening ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在集合和/或模型的构造函数(如果您已定义)中调用 super() 。几天前我遇到了同样的问题:省略调用
super()
无法将_onModelEvent
对象绑定到集合,这导致它在不正确的上下文中调用(this
指向模型而不是集合[反之亦然])。You need to call
super()
in your collection's and/or model's constructor (if you have defined any). I had the same issue a couple of days ago: Omitting callingsuper()
fails to bind the_onModelEvent
object to the collection, which causes it to be invoked in an incorrect context (this
is pointing to the model instead of the collection [and vice versa]).您需要将保存功能绑定到视图。
您可以通过在视图的初始化方法中调用
_.bindAll
来完成此操作You need to bind the save function to the view.
You do this by calling
_.bindAll
in the view's initialize method