视图更改时,Ember.js更新模型

Ember.js update model when view changed?

本文关键字:js 更新 模型 Ember 视图      更新时间:2023-11-10

这里是ember的新手,我想如果你在视图和模型之间绑定数据,那么如果其中一个发生了变化,双方都会同步。

目前,我用Emberfire设置了我的模型,它返回一系列颜色:

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return EmberFire.Array.create({
            ref: new Firebase("https://ember-starter.firebaseio.com/shape")
        });
    },
    setupController: function(controller, model) {
        controller.set('model', model);
    }
});

我的模板设置如下:

<button {{action "add"}}>Draw</button>
{{#each controller}}
    {{#view App.ShapeView contentBinding="content"}}
    <div>{{color}}</div>
    {{/view}}
{{/each}}

我有一个按钮可以为阵列添加新颜色:

App.ApplicationController = Ember.ArrayController.extend ({
    actions: {
        add: function() {
        var newColor = {
          color: "#222222"
          };
          this.pushObject(newColor);
      }
    }
});

在视图中,我设置了一个点击操作来设置颜色属性:

App.ShapeView = Ember.View.extend({
      click: function() {
        var self = this;
        var changeColor = self.set('context.color', '#121212');
    }
  });

使用当前设置,我可以获取/显示颜色列表,并在单击时将颜色更改为#12122。然而,数据并不能持久存在于模型中(firebase)。我想知道是我做错了什么,还是有更好的方法来保存/同步视图和模型之间的更改。

提前感谢!

可能是因为您的add函数中有一个拼写错误。。。this.pushObject(newColore);应该是this.pushObject(newColor);

    add: function() {
    var newColor = {
      color: "#222222"
      };
      this.pushObject(newColor);
  }

祝好运