Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added strict importer. Changed one of the methods from using properti…

…es to options as we have in other places (hope that's ok). Added placeholders for bind and trigger (need to add placeholder for unbind too...) added some event tests that are currently failing - that's ok, they contain expected behavior.
  • Loading branch information...
commit 070aaa177c947fdf04c63696946988aeb8871407 1 parent 5d15f04
Irene Ros authored
Showing with 216 additions and 22 deletions.
  1. +86 −10 ds.js
  2. +75 −0 test/events.js
  3. +52 −11 test/importers.js
  4. +3 −1 test/test.html
View
96 ds.js
@@ -24,6 +24,19 @@
options = options || (options = {});
this._options = options;
+
+ // stores events that are subscribed on this dataset. Format is:
+ // _events["eventName"] = { pos : { rowIds : [], columnIds : [] }, callback : function }
+ this._events = {};
+
+ // if this is a forked dataset, the parent property should be set. We need to
+ // auto subscribe this dataset to sync with its parent.
+ if (options.parent) {
+
+ // TODO: do some auto subscribing here...
+
+ }
+
this._buildData();
};
@@ -46,9 +59,8 @@
if (this._options.strict) {
- // TODO process strict data here
- this._rows = this._options.data.rows;
- this._columns = this._options.data.columns;
+ // If this is a strict format import, use the Strict importer.
+ importer = new DS.Importers.Strict(this._options.data);
} else if (typeof this._options.data !== "undefined") {
@@ -66,23 +78,31 @@
}
},
- filter : function(properties) {
+ filter : function(options) {
var data = {};
_.each(['columns', 'rows', 'metadata'], function(type) {
data[type] = _.clone(this['_'+type]);
}, this);
- if (properties.row) {
- properties.rows = [properties.row];
+ if (options.row) {
+ options.rows = [options.row];
}
- if (properties.rows) {
+ if (options.rows) {
data.rows = _.filter(data.rows, function(row, index) {
- return _.indexOf(properties.rows, index) !== -1;
+ return _.indexOf(options.rows, index) !== -1;
});
}
- return new DS({ data : data, strict : true });
+ // if properties.clone is set to false, return just the
+ // reference to the data.
+ if (typeof options.clone !== "undefined" && !options.clone) {
+ return data;
+ // else return a new dataset of which this is the parent.
+ } else {
+ return new DS({ data : data, strict : true, parent: this });
+ }
+
},
transform : function() {
@@ -174,6 +194,20 @@
iterator( value );
});
});
+ },
+
+ // bind a specific event to a specific callback.
+ bind: function(event, pos, callback) {
+
+ },
+
+ // Trigger callbacks for a specific event. Optionally takes in a position.
+ trigger : function(event, pos) {
+
+ },
+
+ _sync: function(event) {
+
}
});
@@ -209,6 +243,48 @@
});
/**
+ * Handles basic import.
+ * TODO: add verify flag to disable auto id assignment for example.
+ */
+ DS.Importers.Strict = function(data, options) {
+ this._data = data;
+ };
+
+ _.extend(DS.Importers.Strict.prototype, {
+ _buildColumns : function(n) {
+ var columns = this._data.columns;
+
+ // verify columns have ids
+ _.each(columns, function(column) {
+ if (typeof column._id === "undefined") {
+ column._id = _.uniqueId();
+ }
+ });
+
+ return columns;
+ },
+
+ parse : function() {
+ var d = {};
+
+ // Build columns
+ d._columns = this._buildColumns();
+
+ // Build rows
+ d._rows = this._data.rows;
+
+ // verify rows have ids
+ _.each(d._rows, function(row) {
+ if (typeof row._id === "undefined") {
+ row._id = _.uniqueId();
+ }
+ });
+
+ return d;
+ }
+ });
+
+ /**
* Converts an array of objects to strict format.
* @params {Object} obj = [{},{}...]
*/
@@ -254,7 +330,7 @@
var d = {};
// Build columns
- d._columns = this._buildColumns(this._data);
+ d._columns = this._buildColumns();
// Build rows
d._rows = _.map(this._data, function(row) {
View
75 test/events.js
@@ -0,0 +1,75 @@
+$(document).ready(function() {
+
+ module("Events");
+
+ (function() {
+ var obj = { "metadata" : {"name" : "Greek Alphabet" },
+ "columns" : [
+ {"name" : "character", "type" : "string"},
+ {"name" : "is_modern", "type" : "boolean"},
+ {"name" : "name", "type" : "string"},
+ {"name" : "numeric_value", "type" : "number"}
+ ],
+ "rows" : [
+ { data : ["α","alpha",true,1] },
+ { data : ["β","beta",true,2] },
+ { data : ["γ","gamma",true,3] },
+ { data : ["δ","delta",true,4] },
+ { data : ["ε","epsilon",false,5] }]};
+
+ var ds = new DS({ data : obj, strict : true });
+
+ test("basic event binding on entire dataset", function() {
+ expect(1);
+ ds.bind("sampleevent", null, function() {
+ ok(true, "sample event was triggered!");
+ });
+ ds.trigger("sampleevent");
+ });
+
+ test("basic binding on specific row", function() {
+ expect(1);
+ ds.bind("sampleevent2", {row : ds._rows[0]._id} , function() {
+ ok(true, "sampleevent2 triggered with row 1");
+ });
+ ds.trigger("sampleevent2", { row : ds._rows[0]._id });
+ });
+
+ test("basic binding on specific row from array ", function() {
+ expect(1);
+ ds.bind("sampleevent3", {row : ds._rows[0]._id} , function() {
+ ok(true, "sampleevent3 triggered with row 1");
+ });
+ ds.trigger("sampleevent3", { row : [ds._rows[0]._id, ds._rows[1]._id] });
+ });
+
+ test("basic binding on specific row from array reverse", function() {
+ expect(1);
+ ds.bind("sampleevent4", {row : [ds._rows[0]._id, ds._rows[1]._id]} , function() {
+ ok(true, "sampleevent4 triggered with row 1");
+ });
+ ds.trigger("sampleevent4", { row : ds._rows[0]._id });
+ });
+
+ test("multiple subscribers", function() {
+ expect(2);
+ ds.bind("s1", {row : [ds._rows[0]._id, ds._rows[1]._id]} , function() {
+ ok(true, "s1 triggered callback 1");
+ });
+ ds.bind("s1", {row : [ds._rows[0]._id, ds._rows[1]._id]} , function() {
+ ok(true, "s1 triggered callback2");
+ });
+ ds.trigger("s1", { row : ds._rows[0]._id });
+ });
+
+ // TODO: sort out why this is still triggering!
+ test("out of range shouldn't trigger", function() {
+ expect(0);
+ ds.bind("s2", {row : [ds._rows[0]._id, ds._rows[1]._id]} , function() {
+ ok(true, "s2 triggered callback 1");
+ });
+ ds.trigger("s2", { row : ds._rows[2]._id });
+ });
+ }());
+
+});
View
63 test/importers.js
@@ -1,5 +1,21 @@
$(document).ready(function() {
+ function verifyImport(obj, strictData) {
+ // check properties exist
+ ok(typeof strictData._rows !== "undefined", "rows property exists");
+ ok(typeof strictData._columns !== "undefined", "columns property exists");
+
+ // check data size
+ ok(strictData._rows.length === 5, "there are 5 rows");
+ ok(strictData._columns.length === 4, "there are 4 columns");
+
+ // verify all rows have ids
+ _.each(strictData._rows, function(row, i) {
+ ok(typeof row._id !== "undefined", "Row " + i + " has an _id.");
+ });
+
+ };
+
module("Importing Obj");
test("Convert object to dataset", function() {
@@ -12,20 +28,14 @@ $(document).ready(function() {
var importer = new DS.Importers.Obj(obj);
var strictData = importer.parse();
- // check properties exist
- ok(typeof strictData._rows !== "undefined", "rows property exists");
- ok(typeof strictData._columns !== "undefined", "columns property exists");
-
- // check data size
- ok(strictData._rows.length === 5, "there are 5 rows");
- ok(strictData._columns.length === 4, "there are 4 columns");
-
- // check first row
+ verifyImport(obj, strictData);
+
+ // check first row
_.each(obj, function(row, i){
ok(_.isEqual(_.values(row), strictData._rows[i].data), "row " + i + " is equal");
ok(typeof strictData._rows[i].data !== "undefined", "row " + i + " has an id");
});
-
+
// check column types
ok(strictData._columns[0].name === "character", "character is first column");
ok(strictData._columns[0].type === "string", "character is string type");
@@ -34,8 +44,39 @@ $(document).ready(function() {
ok(strictData._columns[2].name === "is_modern", "is_modern is 3rd column");
ok(strictData._columns[2].type === "boolean", "is_modern is boolean type");
ok(strictData._columns[3].name === "numeric_value", "numeric_value is 4th column");
- ok(strictData._columns[3].type === "number", "numeric_value is boolean type");
+ ok(strictData._columns[3].type === "number", "numeric_value is boolean type");
+ });
+
+ module("Strict Importer")
+ test("Basic Strict Import", function() {
+ var obj = { "metadata" : {"name" : "Greek Alphabet" },
+ "columns" : [
+ {"name" : "character", "type" : "string"},
+ {"name" : "is_modern", "type" : "boolean"},
+ {"name" : "name", "type" : "string"},
+ {"name" : "numeric_value", "type" : "number"}
+ ],
+ "rows" : [
+ { data : ["α","alpha",true,1] },
+ { data : ["β","beta",true,2] },
+ { data : ["γ","gamma",true,3] },
+ { data : ["δ","delta",true,4] },
+ { data : ["ε","epsilon",false,5] }]};
+
+ var importer = new DS.Importers.Strict(obj);
+ var strictData = importer.parse();
+ verifyImport(obj, strictData);
+
+ // check column types
+ ok(strictData._columns[0].name === "character", "character is first column");
+ ok(strictData._columns[0].type === "string", "character is string type");
+ ok(strictData._columns[1].name === "is_modern", "is_modern is 2nd column");
+ ok(strictData._columns[1].type === "boolean", "is_modern is boolean type");
+ ok(strictData._columns[2].name === "name", "name is 3rd column");
+ ok(strictData._columns[2].type === "string", "name is string type");
+ ok(strictData._columns[3].name === "numeric_value", "numeric_value is 4th column");
+ ok(strictData._columns[3].type === "number", "numeric_value is number type");
});
});
View
4 test/test.html
@@ -2,7 +2,7 @@
<html>
<head>
<title>Dataset Test Suite</title>
- <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
+ <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />s
<script type="text/javascript" src="vendor/jquery.js"></script>
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
@@ -10,6 +10,8 @@
<script type="text/javascript" src="../ds.js"></script>
<script type="text/javascript" src="tests.js"></script>
<script type="text/javascript" src="importers.js"></script>
+ <script type="text/javascript" src="events.js"></script>
+
</head>
<body>
<div class="dataset-test">
Please sign in to comment.
Something went wrong with that request. Please try again.