Note that there are some explanatory texts on larger screens.

plurals
  1. POBreeze.js: how to transform a Breeze entity to a plain JSON object
    primarykey
    data
    text
    <p>I have an entity instance created using the metadata coming from the server:</p> <pre><code>breeze.NamingConvention.camelCase.setAsDefault(); ... var applicationType = metadataStore.getEntityType('Application'); var application = applicationType.createEntity(); </code></pre> <p>None of the objects in this particular model has circular dependencies.</p> <p>After the user has made some changes to the corresponding object, I need to perform some custom validation on that entity, so I need to convert that object back to its JSON simple form and send it back to a validation Controller (ASP.NET MVC4).</p> <p>The question is how to transform the entity to JSON such that:</p> <ol> <li>The resulting object reflects the naming conventions used in the server side.</li> <li>The object contains simple properties and not knockout observables.</li> <li>And does not contain any other additional property or function used internally by breeze.</li> </ol> <p>I was expecting to find something like:</p> <pre><code>var json = application.toJS(); </code></pre> <p>But such method does not exist. Using ko.toJS(application) also doesn't work because (1), (2) and (3) are not fulfilled.</p> <p>I'm sure this should be trivially easy to do, but I just can't find anything even remotely related on the documentation.</p> <p>UPDATE: You'll forgive me for the horrible hack, but because I'm in a hurry what I did that temporarily solved my problem was just exposing the <code>unwrapEntities</code> internal function of <code>entityManager</code>. I also changed the function definition a little bit (just to exclude that annoying <code>entityAspect</code>):</p> <pre><code>function unwrapEntities(entities, metadataStore, includeEntityAspect) { var rawEntities = entities.map(function(e) { var rawEntity = unwrapInstance(e); if (includeEntityAspect !== undefined &amp;&amp; includeEntityAspect === false) { return rawEntity; } ... }); } </code></pre> <p>And because I have the <code>entityManager</code> available at all times in my services, I was able to extend my types definition to do something like the following:</p> <pre><code>function createApplicant(initialValues) { var applicant = applicantType.createEntity(initialValues); applicant.toJS = function () { var unwrappedEntities = entityManager.unwrapEntities([applicant], entityManager.metadataStore, false); return unwrappedEntities[0]; }; return applicant; } </code></pre> <p>And that's precisely what I need:</p> <pre><code>var json = application.toJS(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload