我如何删除使用带有 Alloy Framework 的 Titanium?

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();
}

就是这样,工作得很好!

任何遇到此问题的人都可以查看此 link 中的完整参考:

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;
            }
        });
    }