From 1be34458067b077d6403eb0bec498759be8fd4ca Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Thu, 19 Jul 2012 16:13:00 +0200 Subject: [PATCH] Some fixes for catalogue loading --- js/app/Catalogue.coffee | 15 +++++++++++---- js/app/Catalogue.js | 25 +++++++++++++------------ js/app/app.coffee | 3 ++- js/app/app.js | 12 ++++-------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/js/app/Catalogue.coffee b/js/app/Catalogue.coffee index c556e5b..aeef42d 100644 --- a/js/app/Catalogue.coffee +++ b/js/app/Catalogue.coffee @@ -7,23 +7,30 @@ class Readings.Catalogue this init: (db) -> + unneded_stmts = (stmt, idx) -> + stmt.indexOf("PRAGMA")==0 or + stmt.indexOf("BEGIN TRANSACTION")==0 or + stmt.indexOf("COMMIT")==0 or + /^\s*$/.exec stmt + if not db? db = @db console.log "initializing DB" db.changeVersion "", Readings.config.get 'db_version' - $.ajax "initdb.sql" + $.ajax Readings.config.get('initdburl'), type: "GET" dataType: 'text' success: (sql) => sql = sql.split /;\n/ - sql = sql.slice(1,-2) + sql = $.grep sql, unneded_stmts, true create = (tx) => for stmt in sql - console.log stmt tx.executeSql stmt, [], (tx,rs) => true, - (tx,err) => console.error(err) + (tx,err) => + console.error("error for #{stmt}") + console.error(err) db.transaction create error: => diff --git a/js/app/Catalogue.js b/js/app/Catalogue.js index 38fbce6..c11357b 100644 --- a/js/app/Catalogue.js +++ b/js/app/Catalogue.js @@ -1,4 +1,3 @@ -// Generated by CoffeeScript 1.3.3 (function() { Readings.Catalogue = (function() { @@ -14,28 +13,30 @@ }; Catalogue.prototype.init = function(db) { - var _this = this; - if (!(db != null)) { - db = this.db; - } + var unneded_stmts, + _this = this; + unneded_stmts = function(stmt, idx) { + return stmt.indexOf("PRAGMA") === 0 || stmt.indexOf("BEGIN TRANSACTION") === 0 || stmt.indexOf("COMMIT") === 0 || /^\s*$/.exec(stmt); + }; + if (!(db != null)) db = this.db; console.log("initializing DB"); db.changeVersion("", Readings.config.get('db_version')); - return $.ajax("initdb.sql", { + return $.ajax(Readings.config.get('initdburl'), { type: "GET", dataType: 'text', success: function(sql) { var create; sql = sql.split(/;\n/); - sql = sql.slice(1, -2); + sql = $.grep(sql, unneded_stmts, true); create = function(tx) { var stmt, _i, _len, _results; _results = []; for (_i = 0, _len = sql.length; _i < _len; _i++) { stmt = sql[_i]; - console.log(stmt); _results.push(tx.executeSql(stmt, [], function(tx, rs) { return true; }, function(tx, err) { + console.error("error for " + stmt); return console.error(err); })); } @@ -62,9 +63,9 @@ }; Catalogue.prototype.map_rs = function(rs, mapper) { - var i, objs, _i, _ref; + var i, objs, _ref; objs = []; - for (i = _i = 0, _ref = rs.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + for (i = 0, _ref = rs.rows.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { objs.push(mapper(rs.rows.item(i))); } return objs; @@ -74,9 +75,9 @@ var rs_to_tags, _this = this; rs_to_tags = function(tx, data) { - var i, tags, _i, _ref; + var i, tags, _ref; tags = []; - for (i = _i = 0, _ref = data.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + for (i = 0, _ref = data.rows.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { tags.push(new Readings.Tag(data.rows.item(i))); } return callback(tags); diff --git a/js/app/app.coffee b/js/app/app.coffee index 2f447d5..78429b2 100644 --- a/js/app/app.coffee +++ b/js/app/app.coffee @@ -15,7 +15,8 @@ Readings.init = -> if Readings.initialized? return Readings.config = new Readings.Config - wlurl: 'http://readings.local' + wlurl: 'http://dev.wolnelektury.pl' + initdburl: 'http://dev.wolnelektury.pl/media/api/mobile/initial/initial.sql' categories: 'author': 'autor', 'epoch': 'epoka', diff --git a/js/app/app.js b/js/app/app.js index 01d1f4a..b95b86e 100644 --- a/js/app/app.js +++ b/js/app/app.js @@ -1,4 +1,3 @@ -// Generated by CoffeeScript 1.3.3 (function() { var rcategory, rtag, rtagid; @@ -17,11 +16,10 @@ }; Readings.init = function() { - if (Readings.initialized != null) { - return; - } + if (Readings.initialized != null) return; Readings.config = new Readings.Config({ - wlurl: 'http://readings.local', + wlurl: 'http://dev.wolnelektury.pl', + initdburl: 'http://dev.wolnelektury.pl/media/api/mobile/initial/initial.sql', categories: { 'author': 'autor', 'epoch': 'epoka', @@ -71,9 +69,7 @@ var tag, tag_id, tag_id_m, _this = this; tag_id_m = rtagid.exec($(this).attr('data-url')); - if (tag_id_m != null) { - tag_id = tag_id_m[1]; - } + if (tag_id_m != null) tag_id = tag_id_m[1]; return tag = Readings.catalogue.withTag(tag_id, function(tag) { return $(_this).Readings('list', { fetch: function(cb) { -- 2.20.1