如何将 where 条件应用于 backbone 查询

How to apply where condition to backbone query

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

所以我构建了这段代码以从数据库中获取数据:

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

现在我想在此集合中应用一个位置。所以我使用这个代码:

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,那么您也在使用 Underscore(或 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; });