Breeze.js is a powerful library you can leverage in your Single Page Applications. It plays nicely with all other technologies, and is designed to provide rich client-side data. You can easily it with your application, regardless of the tech stack. Don’t believe me? Prove otherwise. The authors of Breeze.js have certainly shown you can in their samples – http://www.breezejs.com/samples – (Side note: check out the ESPN sample, whoever made it is a genius)
Mocks –
Most applications have need for mocks. Mocks can be especially powerful when you are building out the front-end and the back-end isn’t entirely ready yet. You can also use them for writing unit tests against data without having to populate from the server. I won’t get into testing in this post because there are so many ways to test and the tests can be very opinionated.
The only requirement for mocks is that you need to have your metadata in place first, either from the server or as the Breeze.js authors mention in the docs you can also get a snapshot of your metadata and pass it in when mocking. This is a great way to iterate and update your models. (Reference – http://www.breezejs.com/documentation/load-metadata-script)
Example –
function loadMocks (manager) { var personMockOne = manager.createEntity('Person', { id: 1, firstName: 'John', lastName: 'Smith' }); var companyMockOne = manager.createEntity('Company', { id: 1, name: 'Acme Inc.' }); companyMockOne.employees.push(personMockOne); }
That’s it! We now have mocks we can run our tests against or build out our pages. Calling loadMocks() and passing in our manager will add the mocks to our entity collection and we can start testing. We pass in the manager to keep our function anonymous, and in the above example I am adding the employee mock to the company’s employees collection, but we could just as easily create the company and then set the company property on the person to companyMockOne. Example –
var companyMockOne = manager.createEntity('Company', { id: 1, name: 'Acme Inc.' }); var personMockOne = manager.createEntity('Person', { id: 1, firstName: 'John', lastName: 'Smith', company: companyMockOne });
You will notice when using this technique that the entityState of the entities are added. If you have logic in your page related to saving or cancelling changes, this could potentially trigger your UI to think the mocks need to be saved. We can easily get around that as well by calling acceptChanges() on the entityAspect of the entity. Example –
var companyMockOne = manager.createEntity('Company', { id: 1, name: 'Acme Inc.' }); companyMockOne.entityAspect.acceptChanges();
This will let Breeze know that you are comfortable with the changes made to the company and they do not need to be persisted to back to the database. While you could also chain the entityAspect.acceptChanges() onto the end of the createEntity method, beware that this will no longer return the entity.
var companyMockOne = manager.createEntity('Company', { id: 1, name: 'Acme Inc.' }).entityAspect.acceptChanges(); // The next line will not console log our entity console.log(companyMockOne);
I often use this method for create enumerations as well that aren’t strictly passed down from a server. This can be especially useful when using someone else’s API where they aren’t exposing all of the data you need. Last Example –
manager.createEntity('Priority', { id: 1, name: 'Severe', imageSource: 'severe.jpg' }).entityAspect.acceptChanges();
This can be especially useful if you are re-using the enum all over your application.
I hope you find this useful, please leave any feedback below, especially if you see any errors. Direct any trouble-shooting questions to StackOverflow.com, and check out the Breeze.js UserVoice for feedback or feature requests.