Skip to content

Commit

Permalink
* introduced joli.model.truncate(), which empties the table associat…
Browse files Browse the repository at this point in the history
…ed to a model

 * introduced joli.record.get(), which allows to acces one record's properties
 * several bugfixes:
   * made joli.query.join() able to complete the joined field names in the format table.field
   * fixed joli.query.whereIn() which was buggy for text values
   * fixed a variable-reference bug in joli.record.save(), which led to an inconsistent behavior of joli.record.isChanged()
  • Loading branch information
xavierlacot committed Jul 22, 2011
1 parent ece3ddb commit e7dddbc
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions joli.js
Expand Up @@ -361,6 +361,10 @@ joli.model.prototype = {
}

return q.execute();
},

truncate: function() {
new joli.query().destroy().from(this.table).execute();
}
};

Expand Down Expand Up @@ -503,7 +507,11 @@ joli.query.prototype = {

if (this.data.join.length > 0) {
joli.each(this.data.join, function(value, key) {
join = join + ' left outer join ' + value[0] + ' ON ' + value[1] + '=' + value[2];
if (-1 === value[1].indexOf('.')) {
value[1] = value[0] + '.' + value[1];
}

join = join + ' left outer join ' + value[0] + ' on ' + value[1] + ' = ' + value[2];
});
}

Expand Down Expand Up @@ -703,7 +711,15 @@ joli.query.prototype = {
this.data.where = '';
}

this.data.where += expression + ' IN ' + value;
if ('array' == joli.getType(value)) {
if (0 === value.length) {
return this;
}

value = '(\'' + value.join('\', \'') + '\')'
}

this.data.where += expression + ' in ' + value;
return this;
}
};
Expand Down Expand Up @@ -752,6 +768,10 @@ joli.record.prototype = {
return this;
},

get: function(key) {
return this[key];
},

isChanged: function() {
if (this.isNew()) {
return false;
Expand All @@ -767,17 +787,20 @@ joli.record.prototype = {
data.originalData = this._originalData;
this._options.table.save(data);
} else if(this.isNew()) {
this._data.id = this._options.table.save(data);
this._data['id'] = this._options.table.save(data);
}

// overwrite original data so it is no longer "dirty" OR so it is no longer new
this._originalData = {};
var newData = {};

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

this._data = this._originalData;
this._data = newData;

this.isNew = function() {
return false;
Expand All @@ -787,7 +810,7 @@ joli.record.prototype = {
},

set: function(key, value) {
this.key = value;
this[key] = value;
this._data[key] = value;
}
};

0 comments on commit e7dddbc

Please sign in to comment.