Skip to content

Commit

Permalink
Improved fetching relational data: not getting 'too much recursion er…
Browse files Browse the repository at this point in the history
…ror' within complex situations anymore (ownage!)
  • Loading branch information
Paul Engel committed Jun 14, 2011
1 parent 5c7ed83 commit ce3b892
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

== Version 0.1.2 (June xx, 2011)

* Being able to fetch relational data both ways (yes!)
* Improved resolving url with a relative path
* Not caching fetched data at default anymore
* Added IE compatibility (defining Array.indexOf and String.trim when missing)
* Being able to fetch relational data both ways (yes!)
* Refactored cache implementation
* Using '1' and 'n' instead of 'one' and 'many' respectively for association cardinality

Expand Down
62 changes: 33 additions & 29 deletions src/csonv.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ Csonv = (function() {
"booleans": function(value) {
return n("boolean", value);
},
"relational": function(values, type, url, id) {
var ids = values.split(Csonv.separators.array);
"relation": function(ids, type, url, id) {
var assoc = type.split(":");
var assoc_url = url.replace(/\w+\.csv$/, assoc[0] + ".csv")
var assoc_url = resolvePath(url, type.split(":")[0] + ".csv");
var array = [];

assoc_url.toObjects(assoc[2], true);
assoc_url.toObjects();
var map = cache[assoc_url].map;

if (assoc[2]) {
for (var key in map) {
var object_map = map[key];
if (object_map[assoc[2]].indexOf(id.toString()) != -1) {
if ((object_map[assoc[2]] || []).indexOf(id.toString()) != -1) {
array.push(object_map._object);
}
}
Expand Down Expand Up @@ -96,40 +95,48 @@ Csonv = (function() {
return request.responseText;
};

var toObjects = function(data, url, exclude) {
var rows = data.split("\n");
var keys = rows.shift().csvSplit();
var types = rows.shift().csvSplit();
var data = [], map = {};
var toObjects = function(data, url) {
cache[url] = {
data: [],
map : {}
};

var $ = cache[url];
var rows = data.split("\n");
var keys = rows.shift().csvSplit();
var types = rows.shift().csvSplit();

for (var i = 0; i < rows.length; i++) {
var row = rows[i].csvSplit(), object = {}, object_map = {};

for (var j = 0; j < keys.length; j++) {
var key = keys[j], parser = parsers[type = types[j]];
var key = keys[j], type = types[j], parser = parsers[type];

if (parser) {
object[key] = parser(row[j], type, url, object.id);
object[key] = parser(row[j], type, url);
} else {
object_map[key] = type.split(":").length == 2 ? row[j].split(Csonv.separators.array) : null;
if (exclude != key) {
object[key] = parsers["relational"](row[j], type, url, object.id);
}
}

}
if (object.id) {
object_map._object = object;
map[object.id] = object_map;
$.map[object.id] = object_map;
}
data.push(object);
$.data.push(object);
}

cache[url] = {
data: data,
map : map
};
for (var i = 0; i < keys.length; i++) {
var key = keys[i], type = types[i], parser = parsers[type];

if (!parser) {
for (var j = 0; j < $.data.length; j++) {
$.data[j][key] = parsers["relation"]($.map[$.data[j].id][key], type, url, $.data[j].id);
}
}
}

return data;
return $.data;
};

return {
Expand All @@ -139,11 +146,8 @@ Csonv = (function() {
array : ","
},
init : defineParsers,
fetch: function(url, exclude, cache) {
if (!cache) {
cache = {};
}
return cache[url] ? cache[url].data : toObjects(ajax(url), url, exclude);
fetch: function(url) {
return cache[url] ? cache[url].data : toObjects(ajax(url), url);
}
};
}());
Expand All @@ -157,8 +161,8 @@ String.trim || (String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
});

String.prototype.toObjects = function(exclude, cache) {
return Csonv.fetch(this, exclude, cache);
String.prototype.toObjects = function() {
return Csonv.fetch(this.toString());
};

String.prototype.csvSplit = function(s) {
Expand Down

0 comments on commit ce3b892

Please sign in to comment.