JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<textarea></textarea>
CSS
html, body {
height: 100%
}
textarea {
width: 90%;
height: 90%;
}
JavaScript
// Base Backbone model
var BaseModel = Backbone.Model.extend({
save: function (attrs, options) {
attrs = attrs || this.toJSON();
options = options || {};
// If model defines serverAttrs, replace attrs with trimmed version
if (this.serverAttrs) attrs = _.pick(attrs, this.serverAttrs);
// Move attrs to options
options.attrs = attrs;
// Call super with attrs moved to options
Backbone.Model.prototype.save.call(this, attrs, options);
}
});
// User model
var User = BaseModel.extend({
url: '/rest/model',
initialize: function () {
// Define server attributes for this model
// COMMENT THIS LINE to verify all attrs are sent
this.serverAttrs = ['first_name', 'last_name'];
}
});
// Instantiate user with two server attributes
// and one UI attribute
var user = new User({
first_name: 'Adam',
last_name: 'Alexander',
visible: 'true'
});
// Show ajax requests in result pane
$.ajax = function (options) {
options = _.pick(options, 'contentType', 'data', 'type', 'url');
$('textarea').text(JSON.stringify(options, null, 2));
}
// Save user
user.save();