All Backbone objects (Models, Collections, Views) can be extended to provide a base that all other objects in the chain can extend from. I'm going to show you some functions that can be extended to make your Backbone models easier for some cases.
If you are extending Backbone objects you may also want to do more than just override the function or property. You can do the following to call the parent function from the extended child function.
toJSON is a Backbone function that exists on all models that gets called before sending data to the server. Here we are calling the default implementation of it and then making sure that all things that are a distance are converted to miles first.
If you are dealing with legacy APIs or systems you don't control, parse can help you normalize your data. Anything you need to do with your data before it hits your Backbone code, you can do here. And don't make it happen in more than once place, especially if every model or collection will need it.
A huge part of Backbone is rendering data from models to the DOM. Backbone provides some easy ways to represent Backbone models as objects which can then be passed to your templating engine of choice. But! A lot of times you don't just want to display your data in the DOM. You want to format it, localize it, etc. You could use `toJSON` to augment the data again but that would only be a good choice if we wanted the same augmentations to happen before sending to the server, which might not be the case. You could also set more properties on the model, but these would get serialized and sent to the server which would not be ideal. For some of our apps, we create a function on the models and collections called
As it says in the Backbone docs, views are almost more convention than code. What that means is that you have a lot of freedom over what Backbone views actually do. This also means that if you have a certain kind of web app (such as a single page app) you might end up doing the same thing for each view. This is where a base view comes in.
The real Backbone render is a no-op, meaning it does nothing. You will probably want your views to do more than nothing! Our team has established a basicRender function which does exactly what it sounds like.
You'll notice in basicRender, we call something called handleBindings. A binding is something we specify on the view in order to say "whenever this property changes, change this on the DOM".
Through the magic of Backbone events and attributes, our contentBindings will be called when the view is created and everything will be added to the DOM and kept in sync whenever it changes.
We create some simple functions that will hide and show our pages and any page will extend this view.
We create some simple functions that will hide and show our pages and any page will extend this view.
Modals (or any other oft repeated interface element) can be cumbersome to create again and again. In this example we create a `renderModal` function that our views can easily create a modal.
One thing I've tried to hack into a Backbone app of mine, was nested models. So Model A has the the data needed to fetch Model B. A lot of time I ended up with very tightly coupled models. The initialize functions were a bevy of listeners and events. So I came up with this solution to try and allow models to have nested models. Also sorry for this code, it's crazy and needs to be cleaned up.