Backbone.js 模型事件未触发

发布于 2024-12-14 00:53:33 字数 1317 浏览 1 评论 0原文

我有以下视图文件:

var BucketTransferView = Backbone.View.extend(
{
initialize: function(args)
{
    _.bindAll(this);
    this.from_bucket = args.from_bucket;
    this.to_bucket = args.to_bucket;
},
events:
{
    'click input[type="submit"]' : 'handleSubmit',
},
render: function()
{
    $(this.el).html(ich.template_transfer_bucket(this.model.toJSON()));
    return this;
},
handleSubmit: function(e)
{
    that = this;

    this.model.save(
        {
            date: 1234567890,
            amount: this.$('#amount').val(),
            from_bucket_id: this.from_bucket.get('id'),
            to_bucket_id: this.to_bucket.get('id')
        },
        {
            success: function()
            {
                // recalculate all bucket balances
                window.app.model.buckets.trigger(
                    'refresh',
                    [that.to_bucket.get('id'), that.from_bucket.get('id')]
                );
            }
        }
    );
    $.colorbox.close();
}
});

我的存储桶集合具有此刷新方法:

refresh: function(buckets)
{
    that = this;
    _.each(buckets, function(bucket)
    {
        that.get(bucket).fetch();
    });
}

我的问题是,当 fetch() 发生并更改集合的模型时,它不会触发具有相同模型的其他视图类中的更改事件。视图的模型具有相同的cid,所以我认为它会触发。

没有发生这种情况的原因是什么?

I've got the following view file:

var BucketTransferView = Backbone.View.extend(
{
initialize: function(args)
{
    _.bindAll(this);
    this.from_bucket = args.from_bucket;
    this.to_bucket = args.to_bucket;
},
events:
{
    'click input[type="submit"]' : 'handleSubmit',
},
render: function()
{
    $(this.el).html(ich.template_transfer_bucket(this.model.toJSON()));
    return this;
},
handleSubmit: function(e)
{
    that = this;

    this.model.save(
        {
            date: 1234567890,
            amount: this.$('#amount').val(),
            from_bucket_id: this.from_bucket.get('id'),
            to_bucket_id: this.to_bucket.get('id')
        },
        {
            success: function()
            {
                // recalculate all bucket balances
                window.app.model.buckets.trigger(
                    'refresh',
                    [that.to_bucket.get('id'), that.from_bucket.get('id')]
                );
            }
        }
    );
    $.colorbox.close();
}
});

My buckets collection has this refresh method:

refresh: function(buckets)
{
    that = this;
    _.each(buckets, function(bucket)
    {
        that.get(bucket).fetch();
    });
}

My problem is that when the fetch() happens and changes the collection's models, it's not triggering change events in other view classes that has the same models in it. The view's models have the same cid, so I thought it would trigger.

What's the reason this doesn't happen?

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

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

发布评论

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

评论(1

浮云落日 2024-12-21 00:53:33

Fetch 将创建新的模型对象。任何与集合绑定的视图都应该绑定到集合的重置事件并重新渲染自身。视图的模型仍将具有相同的 cid,因为它们保存了对旧版本模型的引用。如果您查看 buckets 集合,它可能具有不同的 cid。

我的建议是在渲染存储桶的视图中,您应该渲染所有子视图并保留对这些视图的引用。然后在重置事件中,删除所有子视图并重新渲染它们。

initialize: function()
{
    this.collection.bind('reset', this.render);
    this._childViews = [];
},

render: function()
{
    _(this._childViews).each(function(viewToRemove){
        view.remove();
    }, this);

    this.collection.each(function(model){
        var childView = new ChildView({
            model: model
        });
        this._childViews.push(childView);
    }, this)
}

我希望这对你有用,或者至少让你朝着正确的方向前进。

Fetch will create new model objects. Any view that's tied to the collection should bind to the collection's reset event and re-render itself. The view's models will still have the same cid's because they're holding a reference to an older version of the model. If you look at the buckets collection it probably has different cids.

My suggestion is in the view that renders the buckets, you should render all the child views and keep a reference to those views. then on the reset event, remove all the child views and re-render them.

initialize: function()
{
    this.collection.bind('reset', this.render);
    this._childViews = [];
},

render: function()
{
    _(this._childViews).each(function(viewToRemove){
        view.remove();
    }, this);

    this.collection.each(function(model){
        var childView = new ChildView({
            model: model
        });
        this._childViews.push(childView);
    }, this)
}

I hope this works for you, or at least gets you going in the right direction.

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