AmpersandJS/ampersand-state — pull request #76 by thomashuston (2014-09-22 02:24:31+00:00)
Title: Optionally include derived and session properties in toJSON

Hey &yet!

I'm playing around with using Ampersand in an existing project of mine where I use Hogan mustache templates. The problem I ran into was that `toJSON()` does not include `derived` properties. This makes perfect sense when saving data to the server, but when using `toJSON()` to create a view model to be rendered I've found it limiting.

For example, if I have a `User` model with a `username` property, I might also have a `derived` property `profileURL` that uses the `username` to generate the URL of their profile page. When I render my view, I want to send both the `username` and `profileURL` properties to the template, but by default `toJSON()` only includes `username`. By allowing `toJSON()` (and by extension `serialize()`) to accept an `options` object, I can now optionally include `derived` properties (e.g. `toJSON({derived: true})`).

```javascript
var User = AmpersandState.extend({
props: {
username: 'string'
},
derived: {
profileURL: {
deps: ['username'],
fn: function() {
return '/profile/' + this.username;
}
}
}
});
```

```mustache
{{username}}
```