gpt4 book ai didi

javascript - 无法在Backbone模型listenTo中直接调用fetch

转载 作者:行者123 更新时间:2023-12-02 16:03:44 24 4
gpt4 key购买 nike

我试图让模型监听集合并在集合更改时获取自身:

class Team extends Backbone.Model
urlRoot: '/team',

initialize: function(attributes, options) {

this.listenTo(members, 'change', this.fetch)

提取似乎确实触发了,但是 url 完全困惑了,为了让它工作,我必须将它包装在一个匿名函数中:

this.listenTo(members, 'change', function() {this.fetch();})

有趣的是,当我向模型添加“测试”函数并将 this.fetch() 放入其中时,它可以工作:

this.listenTo(members, 'change', this.test)

test: function() {
this.fetch();
}

为什么我不能在 listenTo 中执行 this.fetch 操作?

最佳答案

每种类型的事件的处理程序都会传递一组特定的参数。 Catalog of Events关于“change”事件有这样的说法:

  • "change" (model, options) — when a model's attributes have changed.

所以如果你这样说:

this.listenTo(members, 'change', this.fetch)

然后 fetch 将被这样调用:

fetch(the_model_that_changed, some_options_object)

但是Model#fetch期望仅使用 options 对象进行调用。结果是 fetch 将在模型实例中查找 options,结果很困惑。

关于javascript - 无法在Backbone模型listenTo中直接调用fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com