-
-
Save renato04/11340340a990d8294a7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp', ['ionic', 'myApp.services', 'myApp.controllers']) | |
.run(function(DB) { | |
DB.init(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp.config', []) | |
.constant('DB_CONFIG', { | |
name: 'DB', | |
tables: [ | |
{ | |
name: 'documents', | |
columns: [ | |
{name: 'id', type: 'integer primary key'}, | |
{name: 'title', type: 'text'}, | |
{name: 'keywords', type: 'text'}, | |
{name: 'version', type: 'integer'}, | |
{name: 'release_date', type: 'text'}, | |
{name: 'filename', type: 'text'}, | |
{name: 'context', type: 'text'} | |
] | |
} | |
] | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp.controllers', ['myApp.services']) | |
.controller('DocumentCtrl', function($scope, Document) { | |
$scope.documents = []; | |
$scope.document = null; | |
// Get all the documents | |
Document.all().then(function(documents){ | |
$scope.documents = documents; | |
}); | |
// Get one document, example with id = 2 | |
Document.getById(2).then(function(document) { | |
$scope.document = document; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Insert the scripts in this order --> | |
<script src="js/ionic.bundle.js"></script> | |
<script src="config.js"></script> | |
<script src="services.js"></script> | |
<script src="controllers.js"></script> | |
<script src="script.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp.services', ['myApp.config']) | |
// DB wrapper | |
.factory('DB', function($q, DB_CONFIG) { | |
var self = this; | |
self.db = null; | |
self.init = function() { | |
// Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production | |
self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1); | |
angular.forEach(DB_CONFIG.tables, function(table) { | |
var columns = []; | |
angular.forEach(table.columns, function(column) { | |
columns.push(column.name + ' ' + column.type); | |
}); | |
var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')'; | |
self.query(query); | |
console.log('Table ' + table.name + ' initialized'); | |
}); | |
}; | |
self.query = function(query, bindings) { | |
bindings = typeof bindings !== 'undefined' ? bindings : []; | |
var deferred = $q.defer(); | |
self.db.transaction(function(transaction) { | |
transaction.executeSql(query, bindings, function(transaction, result) { | |
deferred.resolve(result); | |
}, function(transaction, error) { | |
deferred.reject(error); | |
}); | |
}); | |
return deferred.promise; | |
}; | |
self.fetchAll = function(result) { | |
var output = []; | |
for (var i = 0; i < result.rows.length; i++) { | |
output.push(result.rows.item(i)); | |
} | |
return output; | |
}; | |
self.fetch = function(result) { | |
return result.rows.item(0); | |
}; | |
return self; | |
}) | |
// Resource service example | |
.factory('Document', function(DB) { | |
var self = this; | |
self.all = function() { | |
return DB.query('SELECT * FROM documents') | |
.then(function(result){ | |
return DB.fetchAll(result); | |
}); | |
}; | |
self.getById = function(id) { | |
return DB.query('SELECT * FROM documents WHERE id = ?', [id]) | |
.then(function(result){ | |
return DB.fetch(result); | |
}); | |
}; | |
return self; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment