Backbone.js Tests

by andypotanin

HTML

<script src="http://documentcloud.github.io/backbone/backbone-min.js"></script>

JavaScript

console.clear();

var Document = Backbone.Model.extend({
    toJSON: function() {
        return {
            _source: this.attributes,
            _meta: {
                cid: this.cid,
            },
            _id: this.id,
        };
    },
    idAttribute: "_id",
    drive: function () {
        console.log('driving', this);
    },
    defaults: {
        _type: "document",
            "_meta": {}
    }
});

var Vehicle = Document.extend({
    constructor: function () {
        console.log('created vehicle');
        Backbone.Model.apply(this, arguments);

    },

    drive: function () {
        console.log('driving', this.get('color'), 'vehicle');
    }
});

var Tahoe = new Vehicle({
    _id: 'asdfasdfd',
    type: 'Tahoe',
    color: 'white',
    age: {
        of: ''
    }
});

Tahoe.on('change:color', function (model, color) {
    console.log('color changed');
});

Tahoe.drive();

Tahoe.set({
    color: 'blue'
});

Tahoe.set( 'age.of', 23 );

console.log(Tahoe.toJSON())