What's the correct way to create an unbound instance of a complex type in breeze?

梦想与她 提交于 2019-12-03 22:10:56

问题


So imagine the following model:

class ComplexTypeA
{
    public string ComplexPropertyA
}

class ParentTypeA
{
    public string ParentPropertyA
    public ComplextTypeA ParentPropertyB
}

Now moving into the breeze world on the client-side, assume that for various reasons I need to create an instance of ComplextTypeA and 'attach' (I know it's not attaching, it's copying values, but can't think of another better word) it later to an instance of ParentTypeA I created separately.

Simply put, what's the best way to do this? I have managed to achieve it, but at the moment, it's something like this:

manager.metadataStore.getEntityType('ParentTypeA').complexProperties[0].dataType.createInstance()

If this is the correct way, then cool, and I'll give myself a gold star. I'm worried though that the documentation makes it seem a lot easier, and I've just found a new and creative way to shoot myself in the foot later.

This is actually slightly incorrect, you can create an ‘unbound’ instance of a complexType with the complexType.createInstance method but when you assign it, you are simply copying its values onto an existing instance.

Thanks!


回答1:


See the ComplexType.createInstance method: ComplexType api.

An example might look like:

    // The MetadataStore.getEntityType method returns both EntityTypes and ComplexTypes.
    var locationType = myEntityManager.metadataStore.getEntityType("Location");
    // creates a newLocation complex object with all default values
    var newLocation = locationType.createInstance();
    // or create a fully fleshed out version.
    //  var newLocation = locationType.createInstance( { city: "San Francisco", street: "111 Main Street", state: "CA" });         


来源:https://stackoverflow.com/questions/17544383/whats-the-correct-way-to-create-an-unbound-instance-of-a-complex-type-in-breeze

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!