Jasmine 如何重置钛合金系列?

Jasmine 如何重置钛合金系列?,jasmine,titanium-mobile,titanium-alloy,Jasmine,Titanium Mobile,Titanium Alloy,Backbonejs集合具有用于批量更新的功能。当我与服务器上的JSON数据同步时,我希望在Tianium Alloy中使用此功能,但它似乎没有提交/保存到SQLite—我使用的是sql适配器 config: { columns: { // stuff name: "TEXT" }, adapter: { type: "sql", collection_

Backbonejs集合具有用于批量更新的功能。当我与服务器上的JSON数据同步时,我希望在Tianium Alloy中使用此功能,但它似乎没有提交/保存到SQLite—我使用的是sql适配器

config: {
        columns: {
            // stuff
            name: "TEXT"
        },
        adapter: {
            type: "sql",
            collection_name: "pony",
            db_name: Alloy.CFG.db_name
        }
    }
我有一些茉莉花测试一直不及格。仅供参考,我有一个用于开发的迁移脚本,它向集合中添加了7个项目,这样我就可以使用它了

describe("pony model", function () {
    var Alloy = require("alloy")
            data = {name: "My little pony"},
        collection,
        item;

    beforeEach(function(){
        collection = Alloy.createCollection('pony');
        item = Alloy.createModel('pony');
    });

    // PASSES
    it('can reset all data', function () {
        collection.fetch();
        expect(collection.length).toEqual(7);

        collection.reset(data)
        expect(collection.length).toEqual(1);
     })

     // FAILS
     it('saves reset data', function () {
        collection.fetch();
        expect(collection.length).toEqual(7);

        collection.reset(data)
        collection.fetch()
        expect(collection.length).toEqual(1);
     })

     afterEach(function () {
         item.destroy();
     });
})
这个错误在UI中的表现方式是,当我保存时,当我与服务器同步数据时,TableView会显示新记录,然后当我转到另一个视图并返回到同一个TableView时,同步的数据会消失并替换为默认数据。

我找到的最佳方法(遗憾的是,我不记得我从哪里复制了代码)是手动重置的。我发布了代码来执行此操作:

基本上我自己做SQL
DELETE
,然后做主干
reset()
,然后循环
INSERT到
,最后完成主干
触发器(“fetch”)
事件。通过主干的同步来做这件事会很慢。而正常的
reset()
无论如何都不会运行同步

exports.definition = {

  config: {
    columns: {
      // ...
    },
    adapter: {
      type:            "sql",
      collection_name: "MyModels"
    }
  },

  extendCollection: function(Collection) {

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

    Collection.prototype.saveAll = function(opt) {
      var util    = require("alloy/sync/util");
      var dbName  = this.config.adapter.db_name;
      var table   = this.config.adapter.collection_name;
      var columns = this.config.columns;
      var db      = Ti.Database.open(dbName);

      db.execute("BEGIN;");

      this.forEach(function (model) {
        if (!model.id) {
          model.id = util.guid();
          model.attributes[model.idAttribute ] = model.id;
        }
        var names = [], values = [], q = [];
        for (var k in columns) {
          names.push(k);
          values.push(model.get(k));
          q.push("?");
        }
        var sqlInsert = "INSERT INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");";

        db.execute(sqlInsert, values);
      });

      db.execute("COMMIT;");
      db.close();

      if (!opt || !opt.silent) { this.trigger("reset"); }
      return this;
    };

    Collection.prototype.refreshFromData = function refreshFromData(data) {
      this.destroyAll({silent:true});
      this.reset(data, {silent:true});
      this.saveAll({silent: true});
      this.trigger("fetch");
    };

  }

};

你找到解决方法了吗?没有,我没有-我停止了尝试。Alloy>1.3会出现问题,因为新的Alloy没有包含
Alloy/sync/util.js
模块。我不知道为什么。:(如果Alloy/sync/util.js模块失败,还有什么其他选择?使用外部db文件是唯一的出路?@anish,我不知道,因此“我不明白为什么”评论。我愚蠢地希望比我更聪明的人会愿意启发我们其他人。
util.js
的内容是:
function S4(){return(0 | 65536*(1+Math.random()).toString(16).substring(1)}exports.guid=function(){return S4()+S4()+“-”+S4()+“+S4()”-“+S4()+”-“+S4()+S4()+S4()+S4();};
因此可以在我们的模块中复制上述代码。