本文介绍了我们如何执行自定义转换数据的回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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'),
属性: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链接来演示该问题:



所以如果你更改输入字段并点击回滚,只有名字被回滚。有没有解决方法来实现转换数据的回滚?



谢谢,
Dee

解决方案

希望这样可以节省一些尝试实现同样目标的人的时间,我可以通过编写以下通用转换来进行回滚:

  AS.RawTransform = DS.Transform.extend({
/ **
*循序地读取json对象/数组并将其转换为Ember.Object和Ember.ArrayProxy分别
*
* @param {Object} Javascript对象
* @returns {Object} Ember对象或ArrayProxy
* /
deserialize:function序列化){

if(!(serialized)){
serialized = {};
}

var recursiveDeserializer = function(object,data) {
if(!(object)){
if(Object.prototype.toString.call(data)==='[object Array]'){
object = Ember.ArrayProxy。 createWithMixins(Ember.Sortab leMixin,{content:Ember.A()});
} else if(Object.prototype.toString.call(data)==='[object Object]'){
object = Ember.Object.create();
}
} else {
//当回滚
时使用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,i ++){
object.get('content')。push(recursiveDeserializer(null,data [i]));
}
} else {
return data;
}
返回对象;
};

var ret = recursiveDeserializer(null,serialized);

ret.reopen({
/ **
*此函数返回对Ember对象/数组
* * /
的内容所做的更改
rollback:function(){
var self = this;
Ember.run(function(){
recursiveDeserializer(self,serialized);
});

}
});
return ret;
},

/ **
*循序阅读Ember.Object / Ember.ArrayProxy并将其转换为JavaScript数组或对象
*
* @param {Object} Ember ArrayProxy或Object
* @return {Object} Javascript对象
* * /
serialize:function(deserialized){

var recursiveSerializer = function(object,data){
if(!(object)){
if(data instanceof Ember.ArrayProxy){
object = [];
} else if(data instanceof Ember.Object){
object = {};
}
}

/ **
*无法使用instanceof命令检查类型
*,因为在这一点上的某些原因
*被视为对象,即使它是一个数组
* * /

if(Object.prototype.toString.call(object)==='[object Object] '){
for(var _key in data){
if(data.hasOwnProperty(_key)&&b $ b _key.indexOf('__ ember')< 0&&
_key.indexOf('_ super')< 0&&b $ b 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 recursiveSerializer(null,deserialized);
}
});

这里唯一的缺点是我不能只做model.rollback,我必须在每个'raw'数据类型。例如:model.get('tranformedData')。rollback()



但我认为这至少是一个开始。如果数据很脏,那么也可以在这里添加观察者来检查


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})
    });

and my transformation is defined as :

 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);
        }
    });

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??

Thanks,Dee

解决方案

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);
}
});

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.

这篇关于我们如何执行自定义转换数据的回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:04