javascriptjquerymodeljavascriptmvc

Javascript MVC - How to retain initial values


I need to retain the original values from data returned from a model instance. It seems like there should be a method for returning the unmodified data from the model but I can't find it anywhere. I tried using backup.js but this only works for static string attributes.

$.Model('My.Example.Models.Thing',
/* @Static */
{


},
/* @Prototype */
{   
    getMyStuff : function(args) {
         $.ajax({
            url : '/path/to/my/service,
            type : 'get',
            contentType : 'application/json',
            dataType : 'json',
            success : args.success,
            error : args.error,
            cache : false
        });
    }

...

init: {
    env = this;
}

show: function(){
    thing = new My.Example.Models.Thing();
    thing.getMyStuff({          
        params:params,
        success: function(data){
            // How do you store the original data for later,
            // Without it getting updated when data is modified?
            thing.myStuff = data; 
            env.options.thing = thing;
            env.processMyStuff(thing);  
        },   
        error: function(){
            console.log('error');
        }
    }); 
},

Solution

  • try $.extend({}, data), or $.extend(true, {}, data) for deep copy if you are using jquery > 1.1.4