0
votes

I have a simple breeze/knockout based SPA. I had to implement a "single save" option for a list of entities - editing was to be done on the list not in a separate popup.

On saving the server would check that only certain attrs of the entity were updated and also update a "last updated date" attr.

The list of entities was pulled up from the server by one Entity Manager.

To enable the single save the updated and ready to save entity was exported from the main Entity Manager and imported into a temporary entity manager. The save is performed and (if ok) the object re-exported and imported back into the main entity manager.

function saveSingle(entity)
{
    var tempMgr = MainEntityManager.createEmptyCopy();
    var exp = MainEntityManager.exportEntities([entity]);
    tempMgr.importEntities(exp, { 
        mergeStrategy: breeze.MergeStrategy.PreserveChanges });
    tempMgr.saveChanges()
    .then(function (saveResult) {
        var reImp = tempMgr.exportEntities();
        MainEntityManager.importEntities(reImp);

        entity.Last_Updated_Date(saveResult.entities[0].Last_Updated_Date());
        entity.entityAspect.entityState = breeze.EntityState.Unchanged;

        logger.success("changes saved");
    })
    .fail(function () {
        logger.error("Single Save failed: " + error.message);
    });
}

However, it looks like the new "last updated date" value does not get copied over after the save in the re-export/import back to the MainEntityManager process. I have worked around this but I'm wondering if I've done something wrong in this implementation and whether there is a better way to construct the "single entity save" feature?

Comments and questions welcome!

1

1 Answers

0
votes

I haven't built a Breeze app with multiple EntityManagers yet, but I have read the docs on the subject (it looks like you have too, but if you haven't: http://www.breezejs.com/documentation/multiple-managers).

By the way, looking at your code, I don't think you're doing what they describe in the docs. Perhaps that's intentional, but I thought I would point it out. What they recommend you do is let the user make local edits against entities resolved from your "sandbox" EntityManager, then somehow (perhaps by importing, as you are doing) replicate the change back to the master EntityManager.

Anyhow, I don't think Breeze always sends back a copy of the entity from the server when you save it.
Here's a comment from the EntityManager source:

// Note that the visitAndMerge operation has been optimized so that we do not actually perform a merge if the // the save operation did not actually return the entity - i.e. during OData and Mongo updates and deletes.

To confirm whether or not the server is sending back a copy of your entity from the server, run Fiddler (it's an invaluable tool for all web development) so that you can see exactly what's coming back in the response. Fiddler will even pretty-print a JSON response for you if you ask it to. :-)