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

时间:2013-11-28 00:28:40

标签: javascript ember.js ember-data emberfire

这里是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)中。我想知道我是否做错了什么或者有更好的方法来保存/同步视图和模型之间的变化。

提前感谢!

1 个答案:

答案 0 :(得分:0)

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

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

  }
祝你好运