Ammar Khan
Ammar Khan

Reputation: 2585

Breeze - How to Load Navigation property from cache

I am getting a single entity by using a method fetchEntityByKey, after that I am loading navigation property for the entity by entityAspect.loadNavigationProperty. But loadNavigationProperty always make a call to the server, what I am wondering if I can first check it from cache, if it is exist then get it from there otherwise go the server. How is it possible? Here is my current code

  return datacontext.getProjectById(projectId)
        .then(function (data) {

             vm.project = data;
             vm.project.entityAspect.loadNavigationProperty('messages');
 });

Here is a function that I encapsulated inside datacontext service.

 function getProjectById(projectId) {

            return manager.fetchEntityByKey('Project', projectId)
                .then(querySucceeded, _queryFailed);

            function querySucceeded(data) {

                return data.entity;
            }
        }

Also, how is it possible to load navigation property with some limit. I don't want to have all records for navigation property at once for performance reason.

Upvotes: 3

Views: 571

Answers (2)

Jay Traband
Jay Traband

Reputation: 17052

You can use the EntityQuery.fromEntityNavigation method to construct a query based on an entity and a navigationProperty . From there you can execute the resulting query locally, via the EntityManager.executeQueryLocally method. So in your example once you have a 'project' entity you can do the following.

var messagesNavProp = project.entityType.getProperty("messages");
var query = EntityQuery.fromEntityNavigation(project, messagesNavProp);
var messages = myEntityManager.executeQueryLocally(query);

You can also make use of the the EntityQuery.using method to toggle a query between remote and local execution, like this:

query = query.using(FetchStrategy.FromLocalCache);

vs

query = query.using(FetchStrategy.FromServer);    

Upvotes: 2

user2968607
user2968607

Reputation: 54

please take a look here: http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html as you can see fetchEntityByKey ( typeName keyValues checkLocalCacheFirst ) also have a third optional param that you can use to tell breeze to first check the manager cache for that entity

hope this helps

Upvotes: 0

Related Questions