Problem
You want the client to have a common way to access a server url which is not part of a standard RESTful API.
Solution
Find (or create) the Model or Collection which is what is returned by the API. Add a function to it that takes required arguments and (lastly) jQuery options and returns the jQuery jqxhr. Use this function anywhere in the codebase this endpoint is accessed.
Details
Backbone comes with two network calls which can be modeled after: fetch
and save
. In both cases, the last argument is an options
object which is passed through to jQuery's ajax function, and the functions return the jqxhr
object. All other network requests should have these same characteristics, and can often build on them, fetch
in particular. By building on Backbone's fetch
, the network request responses also automatically update the Model or Collection which is executing the request.
Examples
Each of these functions:
- Overwrites options values that are required for the endpoint.
- Ends with a
fetch
which, in CoffeeScript, returns the result, thejqxhr
.
GET with special URL
class Levels extends CocoCollection
fetchForClassroom: (classroomID, options={}) ->
options.url = "/db/classroom/#{classroomID}/levels"
@fetch(options)
GET by parameter
module.exports = class Campaigns extends CocoCollection
fetchByType: (type, options={}) ->
options.data ?= {}
options.data.type = type
@fetch(options)
Non-GET action for a specific document
module.exports = class Classroom extends CocoModel
removeMember: (userID, opts) ->
options = {
url: _.result(@, 'url') + '/members'
type: 'DELETE'
data: { userID: userID }
}
_.extend options, opts
@fetch(options)
- Home
- Archmage Home
- Artisan Home
- Adventurer Home
- Scribe Home
- Diplomat Home
- Ambassador Home
- Archmage General
- Mission statement
- Coco Models
- Coding Guidelines
- Cookbook
- File system
- JSON Schema
- Technical overview
- Testing
- Third party software and services
- Artisan General
- Building A Level
- Coding Guidelines for Artisans
- Editing Thang Components
- Important Artisan Concepts
- Keyboard Shortcuts
- Artisan How-To Index
- Adventurer General
- Scribe General
- Diplomat General
- i18n
- i18n Glossary nb
- i18n Glossary ru
- i18n Glossary es-419
- Ambassador General
- Dev Setup
- Dev Setup: Linux
- Dev Setup: Windows
- Dev Setup: Mac
- Dev Setup: Vagrant
- Dev Setup: Issues
- Game Engine
- Component
- Multiplayer
- Surface
- System
- Thang
- Thang Component System
- Tome
- World
- Artisan Tabs
- Components And Systems
- Scripts
- Settings
- Thangs
- Other
- Aether
- Client models
- Developer organization
- Educational Standards
- Events, subscriptions, shortcuts
- Chat Room
- Chat Room Rules
- Permissions
- Project Ideas List
- Treema
- Versioning
- Views
CodeCombat | Home | Blog | Forum | Teachers | Legal | Contribute