我正在使用
处理单页应用程序.NET 4.5
Entity Framework 5.0
Knockout 2.2.1
BreezeJS 1.2.5
DurandalJS (Latest Version)
我有一个POCO(通过Code-First映射到EF-5),其中包含一个包含以下字段的街道地址
StreetAddress1
StreetAddress2
City
State
PostalCode
为了便于在客户端上呈现此信息,我使用 getter 创建了一个名为 DisplayAddress 的只读属性,该属性将所有这些字段组合在一起并进行了适当的交易标点符号,间距和任何缺失的项目。
当Breeze拉取实体时,DisplayAddress属性的数据存在于返回的JSON中。但是,当Knockout尝试映射DisplayAddress时,系统会生成以下错误。
Unable to parse bindings.↵Message: ReferenceError: DisplayAddress is not defined;↵Bindings value: text: DisplayAddress
Knockout似乎没有DisplayAddress作为其对象的属性。
有没有人知道为什么Knockout / Breeze没有看到这个属性?
顺便说一句,这是客户端JavaScript和返回的JSON。
define(function (require) {
var app = require('durandal/app');
var system = require('durandal/system');
// Added Company constructor to try and extend breeze entity with display address
var Company = function () {
this.DisplayAddress = ko.observable("");
};
// service name is route to the Web API controller
var serviceName = 'api/CompanyList';
// manager is the service gateway and cache holder
var manager = new breeze.EntityManager(serviceName);
var store = manager.metadataStore;
store.registerEntityTypeCtor("Company", Company);
// define the viewmodel
var vm = {
companys: ko.observableArray(),
save: saveChanges,
show: ko.observable(false),
activate: function () {
return getCompanys();
}
};
// Fetch companies
getCompanys;
//#region private functions
// get companies asynchronously
// returning a promise to wait for
function getCompanys() {
//logger.info("querying Todos");
system.log("querying Companys");
toastr.info("querying Companys");
var query = breeze.EntityQuery.from("Companys");
return manager
.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
// reload vm.companys with the results
function querySucceeded(data) {
system.log("queried Companys");
toastr.success("queried Companys");
debugger;
vm.companys(data.results);
vm.show(true); // show the view
}
};
function queryFailed(error) {
//logger.error("Query failed: " + error.message);
system.log("Query failed: " + error.message);
toastr.error("Query failed: " + error.message);
}
function saveChanges() {
return manager.saveChanges()
.then(function () {
system.log("changes saved");
toastr.success("changes saved");
})
.fail(saveFailed);
}
function saveFailed(error) {
system.log("Save failed: " + error.message);
toastr.error("Save failed: " + error.message);
}
return vm;
//#endregion
});
和JSON
[
{
"$id":"1",
"$type":"USWebDesigners.Data.Entities.Company, USWebDesigners.Data",
"CompanyId":1,
"CompanyName":"Happy Catfish",
"Phone":"601-787-2528",
"CompanyEmail":"howdy@happycatfish.com",
"StreetAddress":"72 Technology Blvd",
"City":"Ellisville",
"State":"MS",
"PostalCode":"39437",
"CompanyUrl":"http://happycatfish.com",
"PortfolioUrl":"http://happycatfish.com/our-work",
"Services":[
],
"SocialLinks":[
],
"PortfolioItems":[
],
"IsLeadParticipant":false,
"IsActive":false,
"CompanyStatus":0,
"SiteQuality":0,
"AverageRating":0.0,
"DisplayAddress":"72 Technology Blvd<br />Ellisville, MS 39437",
"CreatedOn":"2013-03-21T20:10:28.353"
},
{
"$id":"2",
"$type":"USWebDesigners.Data.Entities.Company, USWebDesigners.Data",
"CompanyId":2,
"CompanyName":"Creative Project Solutions, LLC",
"StreetAddress":"170 Pine Grove Rd",
"City":"Heidelberg",
"State":"MS",
"PostalCode":"39439",
"Services":[
],
"SocialLinks":[
],
"PortfolioItems":[
],
"IsLeadParticipant":false,
"IsActive":false,
"CompanyStatus":0,
"SiteQuality":0,
"AverageRating":0.0,
"DisplayAddress":"170 Pine Grove Rd<br />Heidelberg, MS 39439",
"CreatedOn":"2013-03-21T20:10:28.360"
}
]
当然,我确实通过Extending Breeze Entities阅读了,但还没有完全开始工作。
更新
我发现在Breeze带回客户端的元数据中不存在DisplayAddress。所以,我想第一个问题是,如果它是我POCO上的有效属性,为什么未包含在元数据中的未映射属性?
更新2
在进行进一步研究之后,似乎只读属性没有进入服务器上的DBContext元数据。新问题变为:
是否可以覆盖DBContext的元数据以添加到只读属性中?
(为什么我要这样做?因为我希望Breeze能够绑定到只读属性以用于显示目的。)
如果无法向DBContext元数据添加只读属性,那么在客户端添加此属性的最佳解决方案是什么?