client/js/dataloader.js
author rougeronj
Wed, 03 Jun 2015 17:27:46 +0200
changeset 471 e0c7be5dc02c
parent 444 19f0b7803aed
child 472 6dcff4438175
permissions -rw-r--r--
Add a router to handle fragment identifier Set up a listener of the router in the scene to update it Start Backbone.history (eventlistener of the router) when all the project is loaded Include router.js to all the test file

(function(root) {

    "use strict";

    var DataLoader = root.Rkns.DataLoader = {
        converters: {
            from1to2: function(data) {

                //TODO: implement

                return data;
            }
        }
    };


    DataLoader.Loader = function(project, options) {
        this.project = project;
        this.dataConverters = _.defaults(options.converters || {}, DataLoader.converters);
    };


    DataLoader.Loader.prototype.convert = function(data) {
        var schemaVersionFrom = this.project.getSchemaVersion(data);
        var schemaVersionTo = this.project.getSchemaVersion();

        if (schemaVersionFrom !== schemaVersionTo) {
            var converterName = "from" + schemaVersionFrom + "to" + schemaVersionTo;
            if (typeof this.dataConverters[converterName] === 'function') {
                data = this.dataConverters[converterName](data);
            }
        }
        return data;
    };

    DataLoader.Loader.prototype.load = function(data) {
        this.project.set(this.convert(data), {
            validate: true
        });
        Backbone.history.start();
    };

})(window);