#708

A simple playground for learning backbone.js without having to install or configure anything yourself.

HTML

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>
<script src="https://getfirebug.com/firebug-lite.js"></script>

JavaScript

var Item = Backbone.Model.extend({});

var Items = Backbone.Collection.extend({
    model: Item,
    getItem: function() {
        
        // returns and removes the first item from this collection
        if (this.length) {
            var firstItemId = this.toJSON()[0].id;
            itemToReturn = this.get(firstItemId);
            this.remove(itemToReturn);
            return itemToReturn;
        } else {
            console.log('No more items in '+this.name+'.');
            return false;
        }
    }
});

var myItems = new Items([
    { id: 1, name: 'foo1' },
    { id: 2, name: 'foo2' },
    { id: 3, name: 'foo3' },
    { id: 4, name: 'foo4' },
    { id: 5, name: 'foo5' },
    { id: 6, name: 'foo6' },
    { id: 7, name: 'foo7' },
    { id: 8, name: 'foo8' },
    { id: 9, name: 'foo9' }
]);
console.log(myItems.get(1).name);