gpt4 book ai didi

javascript - 避免 `fetch()` 将模型属性标记为已更改

转载 作者:行者123 更新时间:2023-11-29 15:44:06 24 4
gpt4 key购买 nike

我在用户设置配置文件表单中使用 Backbone。我获取模型并将其信息呈现到表单中。当我保存模型所有时,发送的是参数而不是用户更改的参数。

首先我获取模式并将其呈现在 View 上:

// Fetch model and render on view
user = new User({ id: 123 });
user.fetch().done(function () {
view.render();
});

当用户更改字段时,我使用“set()”方法更新了模型:

"change .email": function () {
this.model.set("email", this.email.val());
}

稍后在 View 代码中我保存了一个点击事件(使用 patch: true):

"click .save": function () {
console.log(this.model.changedAttributes()); // Shows all the model attributes
this.model.save({ patch: true }); // Sends all the model attributes
}

如何避免在我使用 fetch 初始化模型后将 Backbone 标记为已更改?

最佳答案

这个 hack 将解决您的问题:

model.fetch({context:model}).done(function () {
this.set({});
view.render();
});

关于javascript - 避免 `fetch()` 将模型属性标记为已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485366/

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