Skip to content

Define your apiRoot once and use it within all your collection and models

Notifications You must be signed in to change notification settings

garethadavies/backbone.apiRoot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Backbone apiRoot

Define your apiRoot once and use it within all your collection and models

Version currently live: v0.1.0

Requirements

Getting up and running

Download the script

Reference the script

This script requires jQuery, Backbone and Underscore, so make sure you add it after those files.

<script src="path/to/file/backbone.apiRoot.js"></script>

Define your api root

You will need to define your api root within your app before any models or collections are included.

Backbone.apiRoot = {

	root: 'http://api.website.com/', // Default: '/'
	dataType: '.json' // Optional

};

The root value is prefixed to all request URLs. If a dataType is supplied, it will be appended to URL.

With this in place, you are now set up to create collections and models.

Using apiRoot

urlSource property

Your collections will now require a urlSource property be set.

This replaces the regular url collection property and is a reference to the section of the api rest pattern that is unique to the collection.

e.g. A list of users is requested from http://api.website.com/users, will now simply be 'users'

Example

var Model = Backbone.Model.extend({

	urlRoot: 'users',

	idAttribute: 'id'

});

var Collection = Backbone.Collection.extend({

	model: Model,

	// Make sure this is set
	urlSource: 'users'

});

// Create our collection
var myCollection = new Collection();

// Fetch the data
myCollection.fetch();

The request for this collection will be made to http://api.website.com/users or http://api.website.com/users.json if a dataType has been set.

// Create our model
var myModel = new Model();

// Set the id of the model
myModel.set({ id: 321 });

// Fetch the data
myModel.fetch();

The request for this model will be made to http://api.website.com/users/321 or http://api.website.com/users/321.json if a dataType has been set.

About

Define your apiRoot once and use it within all your collection and models

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published