保存 Backbone.js 模型时堆栈溢出

发布于 2024-12-21 10:56:44 字数 888 浏览 0 评论 0原文

我有一个简单的 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 技术交流群。

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

发布评论

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

评论(2

未央 2024-12-28 10:56:45

您需要在集合和/或模型的构造函数(如果您已定义)中调用 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 calling super() 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]).

萝莉病 2024-12-28 10:56:45

您需要将保存功能绑定到视图。

您可以通过在视图的初始化方法中调用 _.bindAll 来完成此操作

class Avia.StudentView extends Backbone.View

  initialize: =>
    _.bindAll(@, 'save');

You need to bind the save function to the view.

You do this by calling _.bindAll in the view's initialize method

class Avia.StudentView extends Backbone.View

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