Git Product home page Git Product logo

backbone.apiroot's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.