Skip to content

Commit

Permalink
added a toArray() method on records, and support for record non persi…
Browse files Browse the repository at this point in the history
…stent metadata. Also made fromArray() allow to update existing objects
  • Loading branch information
xavierlacot committed Sep 18, 2011
1 parent 28bbdb2 commit 8d4e4c9
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions joli.js
Expand Up @@ -28,16 +28,12 @@ var joli = {
this.parent = new baseClass(options);

for (prop in this.parent) {
if (this.parent.hasOwnProperty(prop)) {
this[prop] = this[prop] || this.parent[prop];
}
this[prop] = this[prop] || this.parent[prop];
}

// copy base options over
for (opt in this.parent.options) {
if (this.parent.options.hasOwnProperty(opt)) {
this.options[opt] = this.options[opt] || this.parent.options[opt];
}
this.options[opt] = this.options[opt] || this.parent.options[opt];
}
},

Expand Down Expand Up @@ -96,9 +92,7 @@ var joli = {
var mergedOptions = joli.merge(defaults, options);

for (opt in defaults) {
if (defaults.hasOwnProperty(opt)) {
this.options[opt] = mergedOptions[opt];
}
this.options[opt] = mergedOptions[opt];
}
},

Expand Down Expand Up @@ -752,6 +746,7 @@ joli.record = function(table) {
columns: table.getColumns()
};
this._data = {};
this._metadata = {};
};

joli.record.prototype = {
Expand All @@ -765,7 +760,7 @@ joli.record.prototype = {

fromArray: function(data) {
if (data.id) {
this._originalData = {};
this._originalData = { id: data.id };
this.isNew = function() {
return false;
};
Expand All @@ -782,7 +777,7 @@ joli.record.prototype = {
this._data[colName] = null;
this._data[colName] = data[colName];

if (this._originalData) {
if (this._originalData && this.isNew()) {
this._originalData[colName] = data[colName];
}
}, this);
Expand All @@ -803,7 +798,10 @@ joli.record.prototype = {
},

save: function() {
var data = { data: this._data };
var data = {
data: this._data,
metadata: this._metadata
};

if (this.isChanged()) {
data.originalData = this._originalData;
Expand Down Expand Up @@ -834,5 +832,15 @@ joli.record.prototype = {
set: function(key, value) {
this[key] = value;
this._data[key] = value;
},

toArray: function(data) {
var result = [];

joli.each(this._options.columns, function(colType, colName) {
result[colName] = this._data[colName];
}, this);

return result;
}
};

0 comments on commit 8d4e4c9

Please sign in to comment.