如何将where条件应用于主干查询

How to apply where condition to backbone query

本文关键字:查询 应用于 条件 where      更新时间:2023-09-26

我正在用Appcelerator构建一个应用程序。我使用Backbone从数据库中获取数据。

所以我已经建立了这个代码从数据库中获取数据:

var collection = Alloy.createCollection("SocialHistoryDAO");
collection.fetch();

现在我想在这个集合中应用where。所以我使用以下代码:

collection.where(id : 5);

然后这段代码工作,但现在我想应用这个过滤器:

"WHERE ID != 5";

这是可能的吗?

编辑:这是我的SocialHistoryDAO模型:

exports.definition = {
    config: {
        columns: {
            "ID": "INTEGER PRIMARY KEY AUTOINCREMENT",
            "IdOmnia": "INTEGER",
            "DateStart": "text",
            "DateEnd": "text",
            "Quantity": "decimal",
            "Code": "text",
            "CodeSystem": "text",
            "DisplayName": "text",
            "DisplayNameTarget": "text",
            "UnityMeasure": "text",
            "StateID": "INTEGER"
        },
        adapter: {
            type: "sql",
            collection_name: "SocialHistoryDAO",
            idAttribute: "ID"
        }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });
        return Model;
    },
    extendCollection: function(Collection) {
        _.extend(Collection.prototype, {
            destroyAll : function(opt) {
              var db = Ti.Database.open(this.config.adapter.db_name);
              db.execute("DELETE FROM " + this.config.adapter.collection_name);
              db.close();
              this.models = [];
              if (!opt || !opt.silent) { this.trigger("reset"); }
              return this;
            }
        });
        return Collection;
    }
};

如果你在使用Backbone,那么你也在使用下划线(或LoDash)。

这可以通过以下两种方式完成:

var newArray = _.filter(collection.models, function(model) { return model.get('ID') != 5; });

var newArray = _.reject(collection.models, function(model) { return model.get('ID') == 5; });