0

这里是 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');
    }
  });

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

提前致谢!

4

1 回答 1

0

可能是因为您的添加功能中有错字... this.pushObject(newColore); 应该是 this.pushObject(newColor);

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

  }

祝你好运

于 2013-11-28T11:44:19.443 回答