问题
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