Last active
December 11, 2015 10:48
Alloy Migration Test
This file contains 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
var preload_data = [ | |
{title: 'To Kill a Mockingbird', author:'Harper Lee'}, | |
{title: 'The Catcher in the Rye', author:'J. D. Salinger'}, | |
{title: 'Of Mice and Men', author:'John Steinbeck'}, | |
{title: 'Lord of the Flies', author:'William Golding'}, | |
{title: 'The Great Gatsby', author:'F. Scott Fitzgerald'}, | |
{title: 'Animal Farm', author:'George Orwell'} | |
]; | |
migration.up = function(db) { | |
Ti.API.info("First Version: " + JSON.stringify(db)); | |
db.createTable({ | |
columns: { | |
title:"TEXT", | |
author:"TEXT", | |
}, | |
}); | |
for (var i = 0; i < preload_data.length; i++) { | |
db.insertRow(preload_data[i]); | |
} | |
}; | |
migration.down = function(migrationObj) { | |
migrationObj.dropTable("book"); | |
}; |
This file contains 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
migration.up = function(db) { | |
Ti.API.info("Second Version: " + JSON.stringify(db)); | |
var database = Ti.Database.open(db.dbname); | |
database.execute('ALTER TABLE ' + db.table + ' ADD COLUMN isbn INT;'); | |
database.close(); | |
}; | |
migration.down = function(db) { | |
var database = Ti.Database.open(db.dbname); | |
database.execute('CREATE TEMPORARY TABLE book_backup(title,author,alloy_id);') | |
database.execute('INSERT INTO book_backup SELECT title,author,alloy_id FROM ' + db.table + ';'); | |
// Switch between these two lines to try and downgrade the table | |
// First line work, second line does not | |
database.execute('DROP TABLE ' + db.table + ';'); | |
//db.dropTable(); | |
db.createTable({ | |
columns: { | |
title:"TEXT", | |
author:"TEXT", | |
}, | |
}); | |
database.execute('INSERT INTO ' + db.table + ' SELECT title,author,alloy_id FROM book_backup;'); | |
database.execute('DROP TABLE book_backup;'); | |
database.close(); | |
}; |
This file contains 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
exports.definition = { | |
config: { | |
"columns": { | |
"title":"string", | |
"author":"string", | |
}, | |
"defaults" : { | |
"title":"NO TITLE", | |
"author":"NO AUTHOR" | |
}, | |
"adapter": { | |
"type": "sql_new", | |
"collection_name": "book", | |
// comment in and out for migration test | |
// "migration" : 201300182156861, | |
// Using this line to make it easier/faster to test without cleaning up local DB | |
// "db_name":"dumb4" | |
} | |
}, | |
extendModel: function(Model) { | |
_.extend(Model.prototype, { | |
// extended functions go here | |
customProperty: 'book', | |
customFunction: function() { | |
Ti.API.info('I am a book model.'); | |
}, | |
validate: function (attrs) { | |
for (var key in attrs) { | |
var value = attrs[key]; | |
if (key === "title") { | |
if (value.length <= 0) { | |
Ti.API.info(key + ": "+ value.length); | |
return "Error: No title!"; | |
} | |
var res = Alloy.Collections.book.where({title:value}); | |
if (res.length > 0) { | |
Ti.API.info("duplicate"); | |
return "Error: Duplicate title!"; | |
} | |
} | |
if (key === "author") { | |
if (value.length <= 0) { | |
Ti.API.info(key + ": " + value.length); | |
return "Error: No author!"; | |
} | |
} | |
} | |
}, | |
//idAttribute: 'title', | |
//sync : require('adapter').Sync | |
}); // end extend | |
return Model; | |
}, | |
extendCollection: function(Collection) { | |
_.extend(Collection.prototype, { | |
// extended functions go here | |
//sortProperty : 'title', | |
comparator : function(elem) { | |
return elem.get('title'); | |
}, | |
//sync : require('adapter').Sync, | |
//initialize : require('adapter').Init | |
}); // end extend | |
return Collection; | |
} | |
} | |
This file contains 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
$.index.open(); | |
function doClick(e) { | |
Ti.API.info(JSON.stringify(e)); | |
} | |
var library = Alloy.Collections.book; | |
library.fetch(); | |
Ti.API.info(JSON.stringify(library)); | |
$.addButton.addEventListener('click', function(e){ | |
var title = $.titleText.value; | |
var author = $.authorText.value; | |
var book = Alloy.createModel('book', {title: title, author:author}); | |
if (book.isValid()) { | |
book.save(); | |
library.add(book); | |
} | |
}); | |
$.delButton.addEventListener('click', function(e){ | |
if (library.length > 0) { | |
var book = library.pop(); | |
library.remove(book); | |
book.destroy(); | |
} | |
}) |
This file contains 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
".container": { | |
backgroundColor:"white", | |
layout: "vertical" | |
}, | |
"Label": { | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE, | |
color: "#000" | |
}, | |
"TextField" : { | |
left: 10, | |
right: 10, | |
borderColor: "gray", | |
borderWidth: 1, | |
width: 100 | |
}, | |
"View" : { | |
top: 10, | |
bottom: 10, | |
left: 10, | |
layout: "horizontal", | |
height: Ti.UI.SIZE | |
} |
This file contains 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
<Alloy> | |
<Collection src='book'> | |
<Window class="container" layout="vertical" title="My Library" modal="true"> | |
<View> | |
<Label>Title: </Label> | |
<TextField id="titleText"/> | |
<Button id="addButton">Add</Button> | |
</View> | |
<View> | |
<Label>Author: </Label> | |
<TextField id="authorText"/> | |
<Button id="delButton">Remove</Button> | |
</View> | |
<TableView id="tableView" dataCollection="book" onClick="doClick"> | |
<TableViewRow title="{title}"/> | |
</TableView> | |
</Window> | |
</Alloy> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment