我正在为用于Cordova应用程序的Web SQL存储创建一个适配器。到目前为止,除了由于某种原因完全没有调用deleteRecord方法之外,它还是相当不错的。这是我的代码的摘录:
DS.CordovaStorageAdapter = DS.Adapter.extend({
// Other methods...
deleteRecord: function(store, type, record) {
console.info('Deleting!');
var adapter = this;
var qr = new QueryRapper({id: record.get('id')}).tableName(this.tableName(type));
var query = qr.deleteQuery();
console.info(query);
function deleteSuccess(tx, results) {
adapter.didDeleteRecord(store,type,record);
}
return this.db.transaction(
function(tx) { tx.executeSql(query, [], deleteSuccess); },
function(err) { this.dbError(query, err, type, record); }
);
},
// Support methods...
});
这是我的茉莉花测试:
describe('EmberCordovaStorageAdapter', function() {
var m;
beforeEach(function() {
waitForDbInit();
runs(function() {
m = App.TestModel.createRecord({
string: 'String!',
number: 1234,
date: new Date(),
boolean: true
});
m.save();
waitForReady();
});
});
// Other tests...
it('deletes a record', function() {
waitForReady();
runs(function() { console.info('Deleting in test!'); m.deleteRecord(); waitForDeleted(); });
runs(function() { expect(m.get('isDeleted')).toBe(true); });
});
function waitForDeleted(model) {
model = model || m;
waitForMessage(model, 'rootState.deleted.committed');
}
function waitForReady(model) {
model = model || m;
waitForMessage(model, 'rootState.loaded.saved');
}
function waitForMessage(model, msg) {
waitsFor(function() {
console.info(msg, model.get('stateManager.currentPath'));
return model.get('stateManager.currentPath') == msg;
}, 'model message: ' + msg, 1500);
}
});
我在JS控制台中看到消息“正在测试中删除!”但不是“删除!”的消息。 github repo可以在这里找到:https://github.com/eltiare/ember-cordova-storage
答案 0 :(得分:1)
我没有编写自定义适配器的经验,但是快速浏览一下您的测试我看到您的来电deleteRecord
,但我没有看到对commit
的调用
为了让ember-data真正尝试删除你需要调用commit的记录。要提交defaultTransaction,您可以执行以下操作:
m.get('store').commit();