define(["./jquery", "./utils"], function($, utils) {
    "use strict";
    
    function ArrayDataSource(data, delay) {
        this.data = data;
        if(delay) {
            setTimeout(this.load.bind(this), delay);
        } else {
            this.load();
        }
    }
    
    ArrayDataSource.prototype = {
        load: function(data) {
            if(data !== undefined) {
              this.data = data;
            }

            for(var x = 0, l = this.data.length;x<l;x++) {
                if(this.data[x].id === undefined) {
                    this.data[x].id = x;
                }
            }

            $(this).trigger("dataloaded");
            this.ready = true;
        },
        
        recordCount: function() {
            this.assertReady();
            return this.data.length;
        },

        getRowByIndex: function(idx) {
            this.assertReady();
            return this.data[idx];
        },

        getRecordById: function(id) {
            this.assertReady();
            for(var x=0,l=this.data.length; x<l; x++) {
                if(this.data[x].id == id) return this.data[x];
            }
        },

        getData: function(start, end) {
            this.assertReady();
            if(!start && !end) return this.data;
            if(!start) start = 0;
            if(!end) end = this.recordCount();
            return this.data.slice(start, end);
        },

        setValue: function(rowId, key, value) {
            this.assertReady();
            utils.setValue(this.getRecordById(rowId), key, value);
            $(this).trigger("datachanged", { values: [ { id: rowId, key: key } ] });
        },

        assertReady: function() {
            if(!this.isReady()) throw Error("Datasource not ready yet");
        },
        
        isReady: function() {
            return this.ready;
        },

        sort: function(comparator) {
            this.assertReady();
            this.data.sort(comparator);
            $(this).trigger("dataloaded");
        }
    };
    
    return ArrayDataSource;
});

@@ -117,34 +117,8 @@
 ) %7B%0A
-        this.data = data;%0A
     
@@ -175,16 +175,24 @@
 ind(this
+, %5Bdata%5D
 ), delay
@@ -227,20 +227,65 @@
     
-this.load();
+if(data) %7B%0A                this.load(data);%0A            %7D
 %0A   
@@ -652,37 +652,25 @@
     
-$(
 this
-).trigger(%22dataloaded%22)
+.ready = true
 ;%0A  
@@ -679,33 +679,45 @@
         
+$(
 this
-.ready = true
+).trigger(%22dataloaded%22)
 ;%0A      

allow deferred loading of ArrayDataSource