// Backbone.Model.Page.sync
sync: function(method, model, options) {
// Setup CORS
options || (options = {});
if (!options.crossDomain) { options.crossDomain = true; }
// Do "partial" updates and fetch the _source field
if (method === 'update' || method === 'create') {
options.data = JSON.stringify({ "doc": model });
options.type = 'POST';
options.url = model.url() + '/_' + method + \
'?fields=_source&version=' + this.attributes._version;
}
return Backbone.Model.prototype.sync.call(
this, method, model, options
);
}
// Backbone.Model.Page.parse (1)
parse: function(result) {
if (result.get) {
// This was from a create or an update
result._source = result.get._source;
delete result.get;
}
// Use the attributes in the `_source` field for the models attribs
var model = _.extend(
this.defaults, result._source ? result._source : {}
);
// Set the meta fields
_.each(this.meta_fields, function(field) {
if (result[field]) { model[field] = result[field]; }
});
//...
// Backbone.Model.Page.parse (2)
parse: function(result) {
//...
// Check the highlighting
if (result.highlight) {
model.highlight = result.highlight;
}
//...
save: function(key, val, options) {
if (typeof key === 'object') {
key._version = this.attributes._version;
}
return Backbone.Model.prototype.save.call(this, key, val, options);
},
toJSON: function(options) {
var data = _.clone(this.attributes);
// Don't send the meta fields to the server when POST / PUT ing
_.each(this.meta_fields, function(field) {
if (data[field]) {
delete data[field];
}
});
return data;
}