-
-
Save tonylukasavage/4595039 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
$.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 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
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(migrator) { | |
Ti.API.info("First Version: " + JSON.stringify(migrator)); | |
migrator.createTable({ | |
columns: { | |
title:"TEXT", | |
author:"TEXT", | |
}, | |
}); | |
for (var i = 0; i < preload_data.length; i++) { | |
migrator.insertRow(preload_data[i]); | |
} | |
}; | |
migration.down = function(migrator) { | |
migrator.dropTable("book"); | |
}; |
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
migration.up = function(migrator) { | |
var db = migrator.db; | |
Ti.API.info("Second Version: " + JSON.stringify(migrator)); | |
db.execute('ALTER TABLE ' + migrator.table + ' ADD COLUMN isbn INT;'); | |
}; | |
migration.down = function(migrator) { | |
var db = migrator.db; | |
db.execute('CREATE TEMPORARY TABLE book_backup(title,author,alloy_id);') | |
db.execute('INSERT INTO book_backup SELECT title,author,alloy_id FROM ' + migrator.table + ';'); | |
migrator.dropTable(); | |
migrator.createTable({ | |
columns: { | |
title:"TEXT", | |
author:"TEXT", | |
}, | |
}); | |
db.execute('INSERT INTO ' + migrator.table + ' SELECT title,author,alloy_id FROM book_backup;'); | |
db.execute('DROP TABLE book_backup;'); | |
}; |
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
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 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
".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 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
<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