Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - UnMapped property on the Angular/Breeze SPA template

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";}} 

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="";
    }

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".

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?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...