我从 Hot Towel SPA 开始,但使用了其他参考资料,例如您可以在此处找到的 Durandal MovieApp 示例。http://stephenwalther.com/archive/2013/02/08/using-durandal-to-create-single-page-apps.aspx。我还下载并查看了包含示例的微风运行时。
在我的场景中,我使用了带有实体框架的 SQL,并创建了一个 WEBAPI 控制器并遵循了微风的文档。我的控制器除外如下。
[BreezeController]
public class ProjectBillingController : ApiController
{
readonly EFContextProvider<ProjectBillingContext> _contextProvider =
new EFContextProvider<ProjectBillingContext>();
// ~/api/todos/Metadata
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<Client> Clients()
{
return _contextProvider.Context.Clients;
}
...
然后我尝试模仿Durandal Movie APP的代码和目录结构
/App
/App/respositories
/App/repositories/repository.js
/App/viewmodels
/App/viewmodels/clients
/App/viewmodels/clients/show.js
/App/viewmodels/clients/edit.js
/App/viewmodels/clients/create.js
/App/views/clients
/App/views/clients/show.html
/App/views/clients/edit.html
/App/views/clients/create.html
就我而言,我使用了一个存储库,因为它不是任何复杂的查询,尽管对我来说这是一个开始。
但是在我的存储库中,我放置了定义的微风实体管理器和一些函数来检索所有行和 1 行。代码摘录如下,
// repository.js
function getRecordLists(modelsListsObservable, errorObservable, entity) {
return breeze.EntityQuery
.from(entity)
.using(manager).execute()
.then(querySucceeded)
.fail(queryFailed);
function querySucceeded(data) {
modelsListsObservable(data.results);
logger.log('Fetched ' + entity, null, null, true);
}
function queryFailed(error) {
errorObservable("Error retrieving" + entity + " : " + error.message);
logger.error("Error retrieving" + entity + " : " + error.message, null, null, true);
}
};
function getRecord(id, clientObservable, errorObservable, entity, entityKey) {
return breeze.EntityQuery.from(entity)
.where(entityKey, "==", id)
.using(manager).execute()
.then(querySucceeded)
.fail(queryFailed);
function querySucceeded(data) {
clientObservable(data.results[0]);
logger.log('Fetched a record from ' + entity, null, null, true);
}
function queryFailed(error) {
errorObservable("Error retrieving a record from " + entity + ": " + error.message);
logger.error("Error retrieving a record from " + entity + ": " + error.message, null, null, true);
}
};
// show.js
define(function (require) {
var repository = require("repositories/repository");
var app = require('durandal/app');
var router = require("durandal/plugins/router");
var logger = require('services/logger');
var models = ko.observableArray();
var error = ko.observable();
return {
models: models,
error: error,
deleteRecord: deleteRecord,
activate: function (data) {
return repository.getRecordLists(models, error, "Resources");
}
};
我希望这对你有一点帮助。我也在学习这个,有些东西可能不是最佳实践,但它足以让我学习。
谢谢