我正在使用Jaydata和它的Indexeddbprovider,我在添加场景时遇到了问题。
当有多个添加时,只有第一个有效!
$data.Entity.extend("Person", {
Id: { type: "int", key: true, computed: false },
Task: { type: String, required: true, maxLength: 200 },
DueDate: { type: Date },
Completed: { type: Boolean },
University: { type: "int"},
Degree: { type: "int" }
});
$data.EntityContext.extend("ClientDatabase", {
People: { type: $data.EntitySet, elementType: Person }
});
var db = new ClientDatabase({
provider: 'indexedDb', databaseName: 'ClientDB', version: 1
});
var newEntity = {
Id: 1,
Task: 'task1',
DueDate: new Date(),
Completed: false,
University: 1,
Degree: 1
};
var newEntity2 = {
Id: 4,
Task: 'task4',
DueDate: new Date(),
Completed: false,
University: 4
Degree: 4
};
add(db, newEntity, entity1AddedSuccessfully);
function entity1AddedSuccessfully(){
add(db, newEntity2);
}
function add(db, entity, callback){
db.onReady({
success: function () {
db["_People"].add(entity);
db.saveChanges(function () {
if (callback !== undefined) {
callback(entity);
}
});
}
});
}
问题出在这种情况下,newEntity刚刚添加到ClientDB,没有newEntity2!
任何帮助都将不胜感激。
答案 0 :(得分:3)
我在代码中进行了很多更改,您可以在JSFiddle上查看。
重要的事情:
如果您想要实现此行为,请查看JSFiddle代码并分享您的反馈
$data.Entity.extend("Person", {
Id: { type: "int", key: true, computed: true },
Task: { type: String, required: true, maxLength: 200 },
DueDate: { type: Date },
Completed: { type: Boolean },
University: { type: "int"},
Degree: { type: "int" }
});
$data.EntityContext.extend("ClientDatabase", {
People: { type: $data.EntitySet, elementType: Person }
});
var db = new ClientDatabase({
provider: 'indexedDb', databaseName: 'ClientDB', version: 1
});
db.onReady(function(){
var newEntity = new Person( {
Task: 'task1',
DueDate: new Date(),
Completed: false,
University: 1,
Degree: 1
});
var newEntity2 = new Person({
Task: 'task4',
DueDate: new Date(),
Completed: false,
University: 4,
Degree: 4
});
db.People.add(newEntity);
db.saveChanges(function() {
db.People.add(newEntity2);
db.saveChanges(function() {alert(newEntity2.Id);});
});
});
答案 1 :(得分:0)
我害怕你把它混合了一点。 $ data.Entity和$ data.EntityContext用于模型定义
var db = new ClientDatabase(...)
db是一个数据库上下文,它没有EntityContext方法或属性
因此您的代码应如下所示:
db.onReady()
db.People.add(entity)
db.saveChanges(...)
答案 2 :(得分:-1)
我希望这有助于某人...它为我做了诀窍...... http://jaydata.org/forum/viewtopic.php?f=3&t=184