Some fixes for catalogue loading
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Thu, 19 Jul 2012 14:13:00 +0000 (16:13 +0200)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Thu, 19 Jul 2012 14:13:00 +0000 (16:13 +0200)
js/app/Catalogue.coffee
js/app/Catalogue.js
js/app/app.coffee
js/app/app.js

index c556e5b..aeef42d 100644 (file)
@@ -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: =>
index 38fbce6..c11357b 100644 (file)
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
 (function() {
 
   Readings.Catalogue = (function() {
     };
 
     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);
index 2f447d5..78429b2 100644 (file)
@@ -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',
index 01d1f4a..b95b86e 100644 (file)
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
 (function() {
   var rcategory, rtag, rtagid;
 
   };
 
   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) {