VersionOne API 101: Fetch a Story by ID with Backbone.js
The simplest thing that could possibly work! 25 lines of CoffeeScript to use the VersionOne REST API with Backbone.js and its Model class and Backbone.sync API.
by JoshGough
HTML
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script src="https://raw.github.com/powmedia/backbone-forms/master/distribution/backbone-forms.js"></script>
<script src="https://raw.github.com/powmedia/backbone-forms/master/distribution/editors/list.js"></script>
<script src="https://raw.github.com/powmedia/backbone-forms/master/distribution/adapters/backbone.bootstrap-modal.js"></script>
<script src="https://raw.github.com/powmedia/backbone-forms/master/distribution/templates/bootstrap.js"></script>
<h3>"Simplicity--the art of maximizing the amount of work not done--is essential."</h3>
<blockquote>-- <a href="http://agilemanifesto.org" target="_blank">The Agile Manifesto</a></blockquote>
<div id="output"></div>
CoffeeScript
urlRoot = "http://eval.versionone.net/platformtest/rest-1.v1/Data/Story/"
headers = { Authorization: "Basic " + btoa("admin:admin"), Accept: "haljson" }
attributes = ['Name', 'Description', 'Estimate']
storyId = 1154
StoryModel = Backbone.Model.extend(
urlRoot: urlRoot
url: ->
return @urlRoot + @id + '?sel=' + attributes.join ','
fetch: (options) ->
options or (options = { dataType: 'json', headers: headers })
Backbone.Model::fetch.call this, options
)
storyModel = new StoryModel
storyModel.id = storyId
storyModel.fetch().done (data) ->
$('#output').html('<pre>' + JSON.stringify(data, null, 4) + '</pre>')