最も簡単な解決策であると私が信じているのEmber.Object
は、モデルの構造を模倣する を用意することです。編集モードに入るときは、プロパティをモデルから にコピーしEmber.Object
、ユーザーが [保存] をクリックするか、変更を元に戻す操作を行うまで、そこでプロパティを更新します。私が行った重要なことの 1 つは、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');
}
});
次に、このモデルを保存するために、次のようにしました。
注:: ユーザーとホームのコードは、関係のために使用する必要がありました。ユーザーとホームの 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();
}
これが役立つことを願っています。