避免`取()`标记模型的属性改变 [英] Avoid `fetch()` marking model attributes as changed

查看:105
本文介绍了避免`取()`标记模型的属性改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在用户设置配置文件的形式骨干。我取模型,并呈现其信息到表单中。当我保存模型的所有的参数发送,而不是由用户改变的人。

I'm using Backbone in a user settings profile form. I fetch the model and render its information into the form. When I save the model all the parameters are sent instead of the ones changed by the user.

首先,我取模式,并使其对视图:

First I fetch the mode and render it on the view:

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

当用户更改了场我更新了设置()方法模型:

When the user changes a field I updated the model with the 'set()' method:

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

后来在视图code我节省click事件(使用补丁:真正的

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

如何才能避免骨干标志着作为后改变了我使用初始化模式?

推荐答案

此技巧将解决你的问题:

This hack will solve your problem:

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

这篇关于避免`取()`标记模型的属性改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆