我一直在构建我的第一个SPA应用程序,从John Papa的快速启动中开始了很棒的工作。
我遇到的问题是,当我尝试通过微风执行扩展,或者只是选择所有字段时,我在映射我的子集合时遇到了Breeze的问题。 newValue.entityAspect未定义
如果我通过select子句通过我的api查询,一切都很好,如果我直接调用表,'Positions'直接,我在chartVal对象上得到导航错误。
var query = EntityQuery.from('Positions')
//.select('id, endDate, gauge, hoursPerWeek, memberId, title, summary, startDate, totalHours, weightedHours, company, compensation')
.where('memberId', '==', id)
.expand('Company, ChartVals')
.orderBy(orderBy.positions);
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
function querySucceeded(data) {
var list = partialMapper.mapDtosToEntities(manager, data.results, entityNames.position, 'id');
if (observables) {
observables(list);
}
上面的查询将成功,然后调用MapDtosToEntities(由John Papa提供)
function mapDtosToEntities(manager,dtos,entityName,keyName,extendWith){ return dtos.map(dtoToEntityMapper);
function dtoToEntityMapper(dto) {
var keyValue = dto[keyName];
var entity = manager.getEntityByKey(entityName, keyValue);
if (!entity) {
// We don't have it, so create it as a partial
extendWith = $.extend({ }, extendWith || defaultExtension);
extendWith[keyName] = keyValue;
entity = manager.createEntity(entityName, extendWith);
}
mapToEntity(entity, dto);
entity.entityAspect.setUnchanged();
return entity;
}
function mapToEntity(entity, dto) {
// entity is an object with observables
// dto is from json
for (var prop in dto) {
if (dto.hasOwnProperty(prop) && prop !="entityAspect") {
entity[prop](dto[prop]); <-- tanks here on children
}
}
return entity;
}
}
/// <summary>
/// TODO: Update summary.
/// </summary>
public class Position : PersistableObject
{
public int CompanyId { get; set; }
/// <summary>
/// Weighted hours based on Half-Life.
/// </summary>
public int CreditMinutes { get; set; }
public int? CompensationId { get; set; }
public DateTime? EndDate { get; set; }
/// <summary>
/// Code to hold measure of completeness. 10 = complete 0 = not complete.
/// </summary>
public int Gauge { get; set; }
public int? HoursPerWeek { get; set; }
public bool IsCurrent { get; set; }
[StringLength(40)]
public string LinkedInId { get; set; }
[Required]
public int MemberId { get; set; }
[StringLength(40)]
public string Title { get; set; }
[StringLength(400)]
public string WeightedWords { get; set; }
/// <summary>
/// Adjusted Experience score Based on half life
/// </summary>
public int Score { get; set; }
[Required]
[StringLength(2000)]
public string Summary { get; set; }
public DateTime StartDate { get; set; }
public decimal? SalaryEnd { get; set; }
/// <summary>
/// Hourly salary - let users enter hourly, monthly, annual etc
/// </summary>
public decimal? SalaryStart { get; set; }
public decimal TotalHours { get; set; }
/// <summary>
/// Total Man-hours Multiplied by Gauge reading
/// </summary>
public decimal WeightedHours { get; set; }
public int VisibilityId { get; set; }
public Company Company { get; set; }
public Member Member { get; set; }
public virtual Compensation Compensation { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<ChartVal> ChartVals { get; set; }
}
public class ChartVal : PersistableObject
{
[Required]
[StringLength(40)]
public string Key { get; set; }
public double Value { get; set; }
public string Percentage { get; set; }
public int PositionId { get; set; }
public virtual Position Position { get; set; }
}
感谢您花点时间查看我的问题。我很感激你的时间!
答案 0 :(得分:1)
我也见过这个。我相信解决方案将涉及检测属性是否是一个集合并迭代它以逐个添加项目。我没有尝试这样做,因为我试图决定是否正确的要做的事情
我并不打算将此作为答案本身,而是作为其他人跟随海报跟踪相同轨道的指示,并且对社区对此的任何回应感兴趣。
答案 1 :(得分:1)
我遇到了类似的问题,一个具有'imageUser'导航属性的部分用户实体。
我刚刚将John Papa的mapDtosToEntities“mapToEntity”函数更改为此并且它似乎正在工作 - 我是所有这一切的新手所以我无法保证与保存更改相关的导航属性的完整性,但它肯定是映射关联,因为我创建了一个'imageUrl'计算属性,它检索第一个关联并将其显示在图像元素中(即视觉相信):
function mapToEntity(entity, dto) {
// entity is an object with observables
// dto is from json
for (var prop in dto) {
if (dto.hasOwnProperty(prop)) {
if (typeof(dto[prop]) != "object") {
entity[prop](dto[prop]);
} else {
entity[prop] = ko.observable(dto[prop]);
}
}
}
return entity;
}
答案 2 :(得分:0)
最后,我最后放弃了John Papa mapDtosToEntities mapper方法,只是循环遍布我的收藏......
function querySucceeded(data) {
var list = [];
for(i = 0; i< data.results.length; i++)
{
list.push(data.results[i]);
}
observables(list);
}