![](/img/trans.png)
[英]How to open a existing controller in Alloy framework Titanium SDK (Appcelerator)
[英]How may I delete using Titanium with Alloy Framework?
我正在尝试使用Alloy框架仅删除一条记录:
我在控制器.js中创建了以下代码,以显示从ListView获得的变量:
var args = arguments[0] || {};
$.titleLabel.text = args.titulo || 'Default Title';
$.authorLabel.text = args.author || 'Default Author';
并且我创建了此函数以进行删除:
function deleteBook(){
var books = Alloy.createCollection('books');
// The table name is the same as the collection_name value from the 'config.adapter' object. This may be different from the model name.
var table = books.config.adapter.collection_name;
// use a simple query
books.fetch({query:'Delete from ' + table + ' where titulo="' + args.titulo + '"'});
}
我用一个按钮调用了函数,但是出现了这个错误:
[ERROR] : TiExceptionHandler: (main) [359476,359476] ----- Titanium Javascript Runtime Error -----
[ERROR] : TiExceptionHandler: (main) [0,359476] - In alloy/sync/sql.js:1,69
[ERROR] : TiExceptionHandler: (main) [0,359476] - Message: Uncaught TypeError: Cannot call method 'isValidRow' of null
[ERROR] : TiExceptionHandler: (main) [0,359476] - Source: (o)?r.execute(o):r.execute(o.statement,o.params);for(var c=0,u=[];d.isValidRow
[ERROR] : V8Exception: Exception occurred at alloy/sync/sql.js:1: Uncaught TypeError: Cannot call method 'isValidRow' of null
我只希望能够从数据库中删除记录。
我找到了一种删除数据的方法:
在我的案例中,模型是books.js:
deleteRecord : function(opts) {
var collection = this;
var dbName = collection.config.adapter.db_name;
var table = collection.config.adapter.collection_name;
var columns = collection.config.columns;
var names = [], q = [];
for (var k in opts.query.columns) {
names.push(opts.query.columns[k]);
q.push("?");
}
var sql = "DELETE FROM " + table + " " + opts.query.sql;
db = Ti.Database.open(collection.config.adapter.db_name);
db.execute(sql, opts.query.params);
db.close();
collection.trigger('sync');
}
然后在控制器中:
function deleteBook () {
Alloy.Collections.books.deleteRecord({
query : {
sql : "WHERE title=?",
params : args.titulo
}
});
Alloy.Collections.books.fetch();
$.bookdetails.close();
}
就是这样,一切正常!
对于任何有此问题的人,可以在此链接中查看完整的参考:
http://titaniumtuts.blogspot.com.br/2014/05/alloy-collection-crud.html#comment-form
您好,我觉得有人会用这种其他方式删除:
//Method for deleting
var myData= Alloy.Collections.data;
function delete(){
$.dialog.show();
//Dialog before delete
$.dialog.addEventListener('click', function(event) {
switch (event.index) {
case 0:
var note = myData.get(args.myId);
note.destroy();
//Show a toast message
var toast = Ti.UI.createNotification({
message:"deleted",
duration: Ti.UI.NOTIFICATION_DURATION_SHORT
});
toast.show();
$.detail.close();
break;
case 1:
null;
break;
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.