【问题标题】:Model Property Binding in Ember / Ember-DataEmber / Ember-Data 中的模型属性绑定
【发布时间】:2015-06-03 18:46:10
【问题描述】:

对于以下应用程序流程是否有推荐的方法或模式?

  1. 用户输入路线
  2. 检索路线模型并通过模板呈现给用户。
  3. 模板输入绑定到模型属性,因此当用户修改它们时,存储中的数据会即时更新。
  4. 用户在没有点击“保存”的情况下离开路线

我看到的问题是模型保留了用户更改的值,尽管它们没有被持久化到后端。

我目前解决这个问题的方法是这样的,感觉很笨拙:

setupController 钩子中,我使用 Ember.copy 从模型中设置了几个默认属性值来中断绑定:

setupController: function(controller, model) {
    this._super(controller, model);

    var goals = model.get('goals');

    controller.set('goalOne', Ember.copy(defaultGoal.get('goalOne')));
    controller.set('goalTwo', Ember.copy(defaultGoal.get('goalTwo')));
    controller.set('goalThree', Ember.copy(defaultGoal.get('goalThree')));
}

将我的输入绑定到这些值:

{{input value=goalOne type='text'}}

单击保存时,调用updateModel() 方法将这些值设置回模型并保留到后端。

updateModel: function() {
    var model = this.get('content');
    model.set('goalOne', this.get('goalOne');
    model.set('goalTwo', this.get('goalTwo');
    model.set('goalThree', this.get('goalThree');
}

actions: {
    save: function() {
        this.updateModel();
        this.get('content').save();
    }
}

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    查看ember-data-route。它可以让您直接绑定到模型,并在用户在未保存的情况下离开时自动回滚。

    【讨论】:

    • 这看起来是一个非常有用的插件。似乎它应该是默认行为。我要试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多