问题描述
我在 Visual Studio 2012 中使用 Angular/Breeze SPA 模板.我在 TodoList 服务器模型中添加了一个未映射的属性.
I am using the Angular/Breeze SPA template in visual studio 2012. I have added an unmapped property in the TodoList server model.
[NotMapped]
public string MyUnmappedProperty{ get{return "testString";}}
我已经在客户端模型的构造函数中注册了未映射的属性,特别是在像 Ward 在这里建议的 todo.model.js 中:使用微风js和web api处理计算属性
I have registered the unmapped property in the constructor on the client model, specifically in the todo.model.js like Ward suggested here : Handling calculated properties with breezejs and web api
function TodoList() {
this.title = "My todos"; // defaults
this.userId = "to be replaced";
this.myUnmappedProperty="";
}
当第一次在 todo.controller.js 上调用 getTodos() 方法时,myUnmappedProperty"具有在构造函数中设置的空字符串的值.仅在第二次调用 getTodos() 之后(我添加了一个执行此操作的按钮)并将forceRefresh"设置为 true (getTodos(true)) 以查询服务器,然后我才看到 myUnmappedProperty 获取值测试字符串".
When the getTodos() method is called on the todo.controller.js for the first time, the "myUnmappedProperty" has the value of the empty string as set in the constructor. Only after getTodos() is called for the second time( I have added a button that does that) with 'forceRefresh' set to true (getTodos(true)) in order to query the server, only then I see the myUnmappedProperty getting the value "testString".
我想知道为什么我会出现这种行为.是否可以在第一次调用 getTodos() 时用服务器值填充未映射的属性?
I am wondering why I am getting this behaviour. Is it possible to have the unmapped property filled with the server value the first time the getTodos() is called?
谢谢.
推荐答案
你找到这个问题的解决方案了吗?我一直在努力解决同样的问题,最近才发现 这个 SO来自 CassidyK 的帖子,其中他通过更改 Breeze 源代码 (breeze.debug.js) 中的一行来解决一个非常相似的问题.这是他更改的代码片段(摘自 CassidyK 的帖子):
Did you find a solution to this problem? I have been struggling with the same problem and just recently found this SO post by CassidyK, in which he solves a very similar problem by changing a single line in the Breeze source code (breeze.debug.js). Here is the code snippet he changed (taken from CassidyK's post):
proto.initializeFrom = function (rawEntity) {
// HACK:
// copy unmapped properties from newly created client entity to the rawEntity.
// This is so that we don't lose them when we update from the rawEntity to the target.
// Something that will occur immediately after this method completes.
var that = this;
this.entityType.unmappedProperties.forEach(function(prop) {
var propName = prop.name;
that[propName] = rawEntity[propName]; // CassidyK
//rawEntity[propName] = that[propName]; // Breeze
});
if (!this._backingStore) {
this._backingStore = { };
}
};
我已经尝试过这个解决方案,它似乎工作得很好.请注意,如果您的服务器端属性未映射到数据库中,并且在客户端创建对象时需要客户端覆盖此值,则此更改可能会给您带来麻烦.
I have tried this solution and it seems to work just fine. Notice that this change will probably get you into trouble if you ever have a case in which you have a server side property that's not mapped in the database, and need the client side to overwrite this value when the object is created at the client side.
这篇关于Angular/Breeze SPA 模板上的未映射属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!