VersionOne API 101: Backbone.sync with Collections (WIP)

The simplest thing that could possibly work! WIP for using Backbone Collections against the VersionOne 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>

<button id='fetchWithSelect'>Fetch story with select list only</button>
<button id='fetchWithSelectAndWhereFilter'>Fetch stories with select and where filter</button>
<br/>
<br/>
<input type='text' id='filter' disabled value='Visitor can Login' /> <button id='filterAfterSelect' disabled>Filter after fetch</button>

<br />
<br />

<div id="output"></div>

CoffeeScript

# TODO: not all working yet
urlRoot = "http://eval.versionone.net/platformtest/rest-1.v1/Data/Story"
headers = { Authorization: "Basic " + btoa("admin:admin"), Accept: "haljson" }
attributes = ['Name', 'Description', 'Estimate', 'RequestedBy']

saveDefaultOptions = { contentType: 'haljson', patch: true, headers: headers }
fetchDefaultOptions = { dataType: 'json', headers: headers }

formatJson = (data) ->
    return JSON.stringify data, null, 4

updateOutput = (data) ->
    $('#output').html('<pre>' + formatJson(data) + '</pre>')

StoryModel = Backbone.Model.extend(
    urlRoot: urlRoot
    url: ->
        return @urlRoot + @id if @hasChanged() and not @isNew()
        args = {}
        if @select?
            args.sel = @select if _.isString(@select)
            args.sel = @select.join ',' if _.isArray(@select)
        return @urlRoot + id if _.isEmpty args
        return @urlRoot + id + '?' + $.param args
    save: (attributes, options) ->
        options or options = {}
        _.defaults options, saveDefaultOptions
        Backbone.Model::save.call this, attributes, options
    fetch: (options) ->
        options or options = {}
        _.defaults options, fetchDefaultOptions
        console.log options
        if options.select?
            @select = options.select
            delete options.select
        Backbone.Model::fetch.call this, options
)

Stories = Backbone.Collection.extend(
    model: StoryModel
    urlRoot: urlRoot
    url: ->
        args = {}
        val = ''
        val += '/' + @id if @id?
        if @select?
            args.sel = @select if _.isString(@select)
            args.sel = @select.join(',') if _.isArray(@select)
        if @whereFilter?
            args.whereFilter = @whereFilter if _.isString(@whereFilter)
            if _.isObject @whereFilter                
                @whereFilter = $.param @whereFilter
                @whereFilter = @whereFilter.replace('&', ';')
        val += '?' + $.param(args) if not...