Why would the entityAspect of my query items be null?
Asked Answered
G

2

6

Working with breeze backed by SharePoint, as described here, and using TypeScript rather than JS.

In a DataService class I create an EntityManager and execute a Query:

private servicePath: string = '/api/PATH/';
private manager: breeze.EntityManager;

constructor() {
    this.init();
}

private init(): void {
    this.manager = new breeze.EntityManager(this.servicePath);
}

public ListResponses(): breeze.Promise {
    var query = breeze.EntityQuery.from("Responses");
    return this.manager.executeQuery(query);
}

I then call this from my view model, which works fine:

private loadResponses(): void {
    this.dataservice.ListResponses().then((data) => {
        this.handleResponsesLoaded(data);
    }).fail((error) => {
        this.handleDataError(error);
    });
}

private handleResponsesLoaded(data:any): void {
    for (var i = 0; i < results.length; i++){
        this.extendItem(results[i]);
    }
    this.renderList(results, "#tp-responses-list");
}

But my attempt to extend each item fails, because the item's entityAspect is null:

private extendItem(item: any): void {
    item.entityAspect.propertyChanged.subscribe(() => {  // FAILS HERE
        setTimeout(() => {
            if (item.entityAspect.entityState.isModified()) {
                this.dataservice.SaveChanges().then((result) => {
                    tracer.Trace("SaveChanged Result: " + result);
                }).fail((error) => {
                    this.handleDataError(error);
                });
            }
        }, 0);
    });
}

Upon inspecting the result item, I can see it's just the plain data object, with all the properties I would expect but no Entity goodness:

enter image description here

I've just started with breeze, so the best way to put the question is probably: what have I done wrong here?

Giagiacamo answered 20/3, 2013 at 20:24 Comment(0)
S
5

If Breeze can't find a matching type in its metadata to what it receives as a result of a query, it simply returns the "raw" json object.

The reason that your metadata is not available is usually due to one of two explanations:

1) You are not serializing the type information in the query response. The [BreezeController] attribute or the [BreezeJsonFormatter] attributes both accomplish this.

2) The query itself is not returning types for which metadata was described. You can either create the metadata directly on the client in this case, or have it returned from the server via a "Metadata" method. ( see the NoDb example in the Breeze Zip package for an example of the first).

You can also look at the JsonResultsAdapter if you want to coerce any query result into a "known" metadata type, but this is generally not necessary if you are using the [BreezeController] attribute.

Hope this helps.

Shortstop answered 20/3, 2013 at 20:32 Comment(6)
Jay - Thanks for the prompt response. I'm using metadata extracted from the edmx of a service reference, but there must be something wrong with that - I'll try to fix it (I have a lot of entity types and I don't want to apply the metadata manually in the client if I can help it). A quick follow-up therefore: how does breeze match type to metadata? Is it based on the type name or on a map of properties like an interface or ...?Giagiacamo
you can get the list of types that the metadataStore contains via its getEntityTypes method. Each entity type has a 'name' and a 'shortName' property, either can be matched.Shortstop
I'll look at that in the morning. Thanks again.Giagiacamo
Adding tracing to getEntityType helped me find the problem very quickly. See resolution above. Thanks for your help.Giagiacamo
JcFx, we are interested in doing a better job with integrating Breeze with Sharepoint but have a dearth of knowledge about Sharepoint. You seem to have a good deal more. We'd love to get more input on how to best accomplish our goal. Would you be willing to contact me at [email protected].Shortstop
Jay - I'd be glad to. I'm on a deadline today but will drop you an email after the weekend.Giagiacamo
S
2

The following answer is not mine, but OP's. It was posted in the question itself, while it should have an answer.


The problem was a namespace mismatch. I'm extracting metadata from an edmx generated by a Visual Studio service reference. This edmx uses the namespace of the service it is calling. My proxy objects reside in a different namespace. Breeze's entity lookup (even using the short name) is in the format Type#Namespace, and so I was getting no matches. I tweaked my custom BuildJsonMetadata method on the server to swap the namespaces and now I have properly decorated entities which work correctly.

Shelba answered 20/3, 2013 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.