问题描述
我在我的模型之一中使用自定义转换,如下所示:
I use custom transformation in one of my model like this:
App.Question = DS.Model.extend({
questionName: DS.attr('string'),
parentQuestionId: DS.attr('number'),
position: DS.attr('number'),
questionLayoutId: DS.attr('number'),
questionLayoutName: DS.attr('string'),
attributes: DS.attr('raw'),
childQuestions: DS.hasMany('question', {async: true})
});
我的转换定义为:
App.RawTransform = DS.Transform.extend({
deserialize: function (serialized) {
var obbj = Ember.Object.create();
for (var key in serialized) {
obbj.set(key, serialized[key]);
}
return obbj;
},
serialize: function (deserialized) {
return JSON.stringify(deserialized);
}
});
使用此设置,回滚不适用于转换后的属性.这是演示该问题的 jsBin 链接:http://jsbin.com/uBAZOfO/1/
With this setup, the rollback doesnot work for transformed properties. Here is a jsBin link to demo the issue: http://jsbin.com/uBAZOfO/1/
因此,如果您更改输入字段并点击回滚,则只会回滚名称.是否有任何解决方法可以实现转换后的数据回滚?
so if you change the input fields and hit rollback, only name gets rollbacked. Is there any workaround to achieve rollback on the transformed data??
谢谢,迪
推荐答案
希望这将为尝试实现相同目标的人节省一些时间,我能够通过编写以下通用转换来进行回滚:
Hopefully this will save some time for people who are trying to achieve the same, I was able to do rollback by writing following generic transformation:
AS.RawTransform = DS.Transform.extend({
/**
* Recursively read through the json object/array and convert it into Ember.Object and Ember.ArrayProxy respectively
*
* @param {Object} Javascript object
* @returns {Object} Ember Object or ArrayProxy
*/
deserialize: function (serialized) {
if (!(serialized)) {
serialized = {};
}
var recursiveDeserializer = function (object, data) {
if (!(object)) {
if (Object.prototype.toString.call(data) === '[object Array]') {
object = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { content: Ember.A()});
} else if (Object.prototype.toString.call(data) === '[object Object]') {
object = Ember.Object.create();
}
} else {
//used when rolling back
if (Object.prototype.toString.call(data) === '[object Array]') {
var all = object.toArray();
all.forEach(function (item) {
object.removeObject(item);
});
}
}
if (Object.prototype.toString.call(data) === '[object Object]') {
for (var _key in data) {
object.set(_key, recursiveDeserializer(null, data[_key]));
}
} else if (Object.prototype.toString.call(data) === '[object Array]') {
for (var i = 0, len = data.length; i < len; i++) {
object.get('content').push(recursiveDeserializer(null, data[i]));
}
} else {
return data;
}
return object;
};
var ret = recursiveDeserializer(null, serialized);
ret.reopen({
/**
* This function reverts back changes made to the content of Ember Object/Array
* */
rollback: function () {
var self = this;
Ember.run(function () {
recursiveDeserializer(self, serialized);
});
}
});
return ret;
},
/**
* Recursively read through the Ember.Object/Ember.ArrayProxy and convert it into javascript array or object
*
* @param {Object} Ember ArrayProxy or Object
* @return {Object} Javascript object
* */
serialize: function (deserialized) {
var recursiveSerializer = function (object, data) {
if (!(object)) {
if (data instanceof Ember.ArrayProxy) {
object = [];
} else if (data instanceof Ember.Object) {
object = {};
}
}
/**
* Couldn't use instanceof command to check the type
* because for some reason at this point the data
* is seen as Object even if it is an array
* */
if (Object.prototype.toString.call(object) === '[object Object]') {
for (var _key in data) {
if (data.hasOwnProperty(_key) &&
_key.indexOf('__ember') < 0 &&
_key.indexOf('_super') < 0 &&
Ember.typeOf(data.get(_key)) !== 'function'
) {
object[_key] = recursiveSerializer(null, data[_key]);
}
}
} else if (Object.prototype.toString.call(object) === '[object Array]') {
data.forEach(function (d) {
object[object.length] = (recursiveSerializer(null, d));
});
} else {
return data;
}
return object;
};
return recursiveSerializer(null, deserialized);
}
});
这里唯一的缺点是我不能只执行model.rollback,我必须对每个原始"数据类型执行回滚.例如:model.get('transformedData').rollback()
The only drawback here is that I cannot just do model.rollback, I have to perform rollback on each 'raw' data type. eg: model.get('tranformedData').rollback()
但我认为这至少是一个开始.在这里也添加观察者来检查会很好数据是否脏.
But I think this is at-least a start. It would be nice to also add observers here to check if the data is dirty or not.
这篇关于我们如何对自定义转换的数据执行回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!