【问题标题】:How may I delete using Titanium with Alloy Framework?如何使用 Titanium with Alloy Framework 删除?
【发布时间】:2015-02-26 19:44:56
【问题描述】:

我正在尝试使用 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

我只是希望能够从我的数据库中删除记录。

【问题讨论】:

    标签: javascript android titanium titanium-alloy sql-delete


    【解决方案1】:

    我找到了删除数据的方法:

    在我的模型中是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

    【讨论】:

      【解决方案2】:

      你好,我觉得有人会用另一种方式来删除:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-17
        • 2014-12-14
        相关资源
        最近更新 更多