1 Connect Backbone to new collection
Scott Erickson edited this page 2016-04-25 11:57:26 -07:00

Problem

You want the website to use a collection which has been set up on the server.

Solution

Create a Backbone Model in /app/models and a Collection in /app/collections to be used imported and used by any View. Use CodeCombat's subclasses, CocoModel and CocoCollection.

Example

If your new collection has endpoints at /db/widgets, the Model and Collection files would look like this:

/app/models/Widget.coffee

CocoModel = require './CocoModel'
schema = require 'schemas/models/widget.schema'

module.exports = class Widget extends CocoModel
  @className: 'Widget'
  @schema: schema
  urlRoot: '/db/widgets'

/app/collections/Widgets.coffee

Widget = require 'models/Widget'
CocoCollection = require 'collections/CocoCollection'

module.exports = class Widgets extends CocoCollection
  model: Widget
  url: '/db/widgets'

Then you will be able to use Backbone to access and manipulate data in that collection:

Resources