获取一条数据库记录,显示,更新并将其保存回数据库
问题描述:
已解决!这是一个淘汰赛问题(错误的绑定)。但也许有人喜欢争论或评论一般的代码(dataservice,viewmodel等)。获取一条数据库记录,显示,更新并将其保存回数据库
我试着构建一个Breeze示例,在那里我得到一个数据库记录(使用fetchEntityByKey),显示它进行更新,然后使用保存按钮将更改写回数据库。我无法弄清楚如何让它起作用。
我试图具有的DataService(“类”)和视图模型(“类”),用基因敲除的视图模型绑定到视图。
我非常感谢,如果有人可以提供样品或提供一些提示。
Thankx,哈利
var dataservice = (function() {
var serviceName = "/api/amms/";
breeze.NamingConvention.camelCase.setAsDefault();
var entityManager = new breeze.EntityManager(serviceName);
var dataservice = {
serviceName: serviceName,
entityManager: entityManager,
init: init,
saveChanges: saveChanges,
getLocation: getLocation
};
return dataservice;
function init() {
return getMetadataStore();
}
function getMetadataStore() {
return entityManager.fetchMetadata()
.then(function (result) { return dataservice; })
.fail(function() { window.alert("fetchMetadata:fail"); })
.fin(function() { });
}
function saveChanges() {
return entityManager.saveChanges()
.then(function (result) { return result; })
.fail(function() { window.alert("fetchEntityByKey:fail"); })
.fin(function() { });
}
function getLocation() {
return entityManager.fetchEntityByKey("LgtLocation", 1001, false)
.then(function (result) { return result.entity; })
.fail(function() { window.alert("fetchEntityByKey:fail"); })
.fin(function() { });
}
})();
var viewmodel = (function() {
var viewmodel = {
location: null,
error: ko.observable(""),
init: init,
saveChanges: null
};
return viewmodel;
function init() {
return dataservice.init().then(function() {
viewmodel.saveChanges = dataservice.saveChanges;
return getLocation();
})
}
function getLocation() {
return dataservice.getLocation().then(function (result) {
return viewmodel.location = result;
})
}
})();
viewmodel.init().then(function() {
ko.applyBindings(viewmodel);
});
答
高兴你解决它。不禁注意到您添加了大量无用的回调。我想不出有这样的理由。您还明确要求提供元数据。但是你打给fetchEntityByKey
的电话会隐式地为你做,因为如你所说的,它总是会去服务器。
此外,在数据服务中重新抛出失败回调中的错误是一个好主意,这样调用者(例如ViewModel)可以添加自己的失败处理程序。没有重新抛出,调用者的失败回调将不会听到它(Q保证机器就好像第一个失败处理程序“解决了”问题一样)。
因此,您的DataService可以减少到:
var dataservice = (function() { breeze.NamingConvention.camelCase.setAsDefault(); var serviceName = "/api/amms/"; var entityManager = new breeze.EntityManager(serviceName); var dataservice = { serviceName: serviceName, // why are you exporting this? entityManager: entityManager, saveChanges: saveChanges, getLocation: getLocation }; return dataservice; function saveChanges() { return entityManager.saveChanges() .fail(function() { window.alert("saveChanges failed: " + error.message); throw error; // re-throw so caller can hear it }) } function getLocation() { return entityManager.fetchEntityByKey("LgtLocation", 1001, false) .then(function (result) { return result.entity; }) .fail(function() { window.alert("fetchEntityByKey failed: " + error.message); throw error; // re-throw so caller can hear it }) } })();
我不想作过多的这一点。也许你会给我们更精彩的东西。但是,如果你(或读者)认为这些方法总是必要的,我想表明他们不是。
你能粘贴一个代码示例吗?您是否收到saveChanges调用的错误? – Richard 2013-03-03 19:59:24
嗨理查德...我已经添加了上面的代码。不,我没有从调用saveChanges中得到一个错误 – iwhp 2013-03-04 14:32:34
嗨,理查德......这不是Breeze的编码问题,我在Knockout上做了一些错误。也许你想评论上面的代码... Thankx! – iwhp 2013-03-04 15:40:42