Creating custom EJSON objects
It's pretty easy to pass simple objects between the client and the server using DDP. But did you know that you can pass custom, named objects complete with methods? The folks at Meteor have extended JSON to allow customized, complete objects to be passed over DDP, without you having to worry about serialization/deserialization. This recipe will teach you how to create and use custom EJSON objects, and pass those custom objects between the client and the server.
Getting ready
We will be using the previous recipe found in this chapter, Building custom server methods, as a baseline. Please complete that recipe, and then make the following modifications.
Declaring the Swatch object
Create a new file named [project root]/both/swatch.js
and add the following code to the file:
Swatch = function (color){ this.color = color; } Swatch.prototype = { constructor: Swatch, switch: function(){ this.color = randomColor(); }, toString: function(){ return...