5

I'm not sure if it's the correct way to express my requirement. But the word "fork" appears in the roadmap of Ember Data github page. And it's a killer feature in EPF. I'm wondering if I can do it in Ember Data.

The fork feature is useful when we have an edit page and bind a model. When we edit the information, I don't want the model properties to be changed because if the model properties are also displayed in other place they will be changed automatically. That's not what I want.

An example is a list on the left side of the page and a edit form for a specific model on the right side of the page. When I modify role name in the text field, the role name on the left side is changed because of data binding.

enter image description here

EPF solves this problem by "fork" the existing model and set it in a child session. The session in EPF is similar with store in Ember Data. When you modify the forked model it does not effect the model in the main session. After the forked model is updated it can be merged back to main session, and the corresponding model in main session are updated.

What I can think a solution in Ember Data is to create a different store and copy the model to that store. But it is a bit complicated. Does anyone have a better solution? I searched on stackoverflow and ember discuss forum and didn't find an answer.

4

2 回答 2

1

我不确定在 Ember 中是否有标准通用的方法来执行此操作,但我已经编写了一个 Mixin,我可以将它放在我的路线上,以提供模型的一些基本“缓冲”:

App.BufferedRouteMixin = Ember.Mixin.create({
    setupController: function(controller, model) {
        this.setBufferFromModel(controller, model);
        this._super(controller, model);
    },
    setBufferFromModel: function(controller, model) {
        var buffer = {};
        controller.set('model', model);
        model.eachAttribute(function(name, meta) {
            buffer[name] = model.get(name);
        });

        controller.set('buffer', buffer);
    },
    setModelFromBuffer: function() {
        var model = this.get('model'),
            buffer = this.get('buffer');

        model.eachAttribute(function(name, meta) {
            if (buffer[name]) {
                model.set(name, buffer[name]);
            }
        });
    }
});

将此添加到我的编辑路线后,我可以调用setModelFromBuffer我的save操作。在我的模板中,我可以使用{{#with buffer}}助手。

于 2014-05-06T14:19:09.970 回答
-1

我认为最简单的解决方案是拥有一个Ember.Object模仿模型结构的模型。进入编辑模式时,将模型中的属性复制到模型中Ember.Object,然后让它们在那里更新,直到用户单击“保存”或您希望将更改合并回的任何操作。我做的一件重要的事情是添加混入Ember.Copyable我的对象。下面是我用来为自己解决这个问题的一些代码。

注意::此代码是为了防止模型在提交之前被创建,所以我的不是编辑,而是创建新的。

App.SomeModel = DS.Model.extend({
  user: DS.belongsTo('user'),
  home: DS.belongsTo('home'),

  cost: DS.attr('number'),
  title: DS.attr('string'),
  description: DS.attr('string'),
  category: DS.attr('number'),
  categoryName: function () {
    return Roomy.enums.TransactionCategories[this.get('category')]
  }.property('category'),
  date: DS.attr('date', {
       defaultValue: function() { return new Date(); }
  }),
  fuzzyDate: function () {
    return moment(this.get('date')).fromNow();
  }.property('date'),
  split: DS.attr('boolean'),
  contributors: DS.attr('array'),
  points: DS.attr('number')
});

App.SomeModelNew = Ember.Object.extend(Ember.Copyable, {
  user: null,
  home: null,
  cost: null,
  title: null,
  description: null,
  category: null,
  date: new Date(),
  split: false,
  contributors: [],
  points: null,
  copy: function () {
    return this.getProperties('cost', 'title', 'description', 'category', 'date', 'split', 'contributors', 'user', 'home');
}
});

然后为了保存这个模型,我做了这样的事情。
注意::由于关系,我不得不使用带有 User 和 Home 的代码,简单地复制 User 和 Home 的 json 形式不会持久化关系并为模型提供它在数据库中所需的 ID。

控制器代码如下:

//Before this function is called, all the inputs in the form have been filled in and the instance now has values for all the fields that were defined for it
saveTxn: function (txn) {
    // txn is the App.SomeModelNew instance
    copy = this.store.createRecord('transaction', txn); // this returns the App.SomeModelNew.copy() object
    copy.set('user', txn.get('user')); // OVerwrite user for relationship
    copy.set('home', txn.get('home')); // Overwrite home for relationship
    return copy.save();
}

我希望这有帮助。

于 2014-05-06T18:11:48.963 回答