1

我刚刚开始使用 ember.js。我的应用程序中有两个模型。一个保存数据,一个保存用户编辑的数据。我使用单向绑定来绑定它们。

App.ViewModel = Ember.Object.create({
  title:'title',
  text:'text',
)};

App.EditModel = Ember.Object.create({
  titleBinding: Ember.Binding.oneWay('App.ViewModel.title'),
  textBinding: Ember.Binding.oneWay('App.ViewModel.text'),
)};

我让用户在 EditModel 模型中编辑数据。但是,如果用户放弃更改,我希望能够将值设置回编辑前的状态,即。ViewModel 中的值。

有没有办法重新绑定这些属性?或者手动增加 ViewModel 中属性的更改事件,以便更新 EditModel?或任何其他方法来解决我的问题?

4

1 回答 1

3

您可以创建一个自定义 Mixin 来处理模型的重置,请参阅http://jsfiddle.net/pangratz666/CjB4S/

App.Editable = Ember.Mixin.create({
    startEditing: function() {
        var propertyNames = this.get('propertyNames');
        var props = this.getProperties.apply(this, propertyNames);
        this.set('origProps', props);
    },
    reset: function() {
        var props = this.get('origProps');
        Ember.setProperties(this, props);
    }
});


App.myModel = Ember.Object.create(App.Editable, {
    propertyNames: ['title', 'text'],
    title: 'le title',
    text: 'le text'
});

稍后在视图中,startEditing当您想要拍摄当前值的快照以及reset想要重置为值的先前快照时,您只需调用 。

于 2012-04-16T11:12:57.960 回答