From c144dda731b1519843e37c96eaf1c8859d2bf727 Mon Sep 17 00:00:00 2001 From: rgrp Date: Thu, 3 Nov 2011 09:00:42 +0000 Subject: [PATCH] [model/dataset][m]: introduce Model module containing basic Dataset and TabularData objects (plus tests). * NB: current Dataset is basic one running off local data. --- src/dataset.js | 43 +++++++++++++++++++++++++++++++++++++++++++ test/dataset.test.js | 39 +++++++++++++++++++++++++++++++++++++++ test/index.html | 7 ++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/dataset.js create mode 100644 test/dataset.test.js diff --git a/src/dataset.js b/src/dataset.js new file mode 100644 index 00000000..842a6840 --- /dev/null +++ b/src/dataset.js @@ -0,0 +1,43 @@ +this.RECLINE = this.RECLINE || {}; + +RECLINE.Model = function ($, _, Backbone) { + var my = {}; + + // A Dataset model. + my.Dataset = Backbone.Model.extend({ + initialize: function(data, rawTabularData) { + this.tabularData = new my.TabularData(rawTabularData); + } + + // get TabularData object associated with this Dataset + // + // async (as may involve getting data from the server) implementing standard promise API + , getTabularData: function() { + var dfd = $.Deferred(); + dfd.resolve(this.tabularData); + return dfd.promise(); + } + }); + + // TabularData model + my.TabularData = Backbone.Model.extend({ + getLength: function() { + return this.get('rows').length; + } + , getRows: function(numRows, start) { + if (start === undefined) { + start = 0; + } + if (numRows === undefined) { + numRows = 10; + } + var dfd = $.Deferred(); + var results = this.get('rows').slice(start, start+numRows); + dfd.resolve(results); + return dfd.promise(); + } + }); + + return my; +}(this.jQuery, this._, this.Backbone); + diff --git a/test/dataset.test.js b/test/dataset.test.js new file mode 100644 index 00000000..a2bd3d86 --- /dev/null +++ b/test/dataset.test.js @@ -0,0 +1,39 @@ +(function ($) { + +module("Dataset"); + +test('new Dataset', function () { + var metadata = { + title: 'My Test Dataset' + , name: '1-my-test-dataset' + , id: 1 + }; + var indata = { + headers: ['x', 'y', 'z'] + , rows: [ + {x: 1, y: 2, z: 3} + , {x: 2, y: 4, z: 6} + , {x: 3, y: 6, z: 9} + , {x: 4, y: 8, z: 12} + , {x: 5, y: 10, z: 15} + , {x: 6, y: 12, z: 18} + ] + }; + var dataset = new RECLINE.Model.Dataset(metadata, indata); + equal(dataset.get('name'), metadata.name); + expect(6); + setTimeout(2); + dataset.getTabularData().then(function(tabularData) { + equal(tabularData.get('headers'), indata.headers); + equal(tabularData.getLength(), 6); + tabularData.getRows(4, 2).then(function(rows) { + equal(rows[0], indata.rows[2]); + }); + tabularData.getRows().then(function(rows) { + equal(rows.length, Math.min(10, indata.rows.length)); + equal(rows[0], indata.rows[0]); + }); + }); +}); + +})(this.jQuery); diff --git a/test/index.html b/test/index.html index 9812c71a..f76e3359 100644 --- a/test/index.html +++ b/test/index.html @@ -3,9 +3,14 @@ Qunit Tests - + + + + + +

Qunit Tests