22. モデル
exports.definition = {
config: {
"columns": {
"item":"text",
"done":"integer",
"date_completed":"text"
},
"adapter": {
"type": "sql",
"collection_name": "todo"
}
},
extendModel : function(Model) {
_.extend(Model.prototype, {
validate : function(attrs) {
for (var key in attrs) {
var value = attrs[key];
if (value) {
if (key === "item") {
if (value.length <= 0) {
return 'Error: No item!';
}
}
if (key === "done") {
if (value.length <= 0) {
return 'Error: No completed flag!';
}
}
}
}
}
});
return Model;
},
extendCollection : function(Collection) {
_.extend(Collection.prototype, {
comparator: function(todo) {
return todo.get('done');
}
});
return Collection;
}
・Backbone.jsライク
・Sqliteが簡単に使える!
・generateで雛形もつくれる
23. コントローラー
function whereFunction(collection) {
return !whereIndex ?
collection.models :
collection.where({ done: whereIndex === 1 ? 0 : 1 });
}
// Perform transformations on each model as it is processed. Since
// these are only transformations for UI representation, we don't
// actually want to change the model. Instead, return an object
// that contains the fields you want to use in your bindings.The
// easiest way to do that is to clone the model and return its
// attributes with the toJSON() function.
function transformFunction(model) {
var transform = model.toJSON();
transform.item = '[' + transform.item + ']';
return transform;
}
// open the "add item" window
function addToDoItem() {
Alloy.createController("add").getView().open();
}
// Show task list based on selected status type
function showTasks(e) {
if (typeof e.index !== 'undefined' && e.index !== null) {
whereIndex = e.index; //TabbedBar
} else {
whereIndex = INDEXES[e.source.title]; // Android menu
}
todos.fetch();
}
・Controllerはjsでごりごり