A Backbone.js Playground

a playground setup with includes of backbone, jquery, underscore,...

by Zuriel

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>

JavaScript

AppEvents = {};
_.extend(AppEvents, Backbone.Events)
    
var User = Backbone.Model.extend({
    initialize: function(){
        AppEvents.on('my_event', this.listen, this);
    },

    listen: function(){
        console.log("%s still listening...", this.get('name'));
    },
    
    unlink: function(){
        AppEvents.off( null, null, this );
    }
});

var Users = Backbone.Collection.extend({
    model: User,
    
    cleanUp: function( data ){
        this.each( function( model ) { model.unlink(); } );
        this.reset( data );
    }
});


// testing
var users = new Users([{name: 'John'}]);
console.log('users.size: ', users.size());

AppEvents.trigger('my_event');

users.cleanUp();
console.log('users.size: ', users.size());

AppEvents.trigger('my_event');