Last active
December 12, 2015 05:39
-
-
Save daf/4723738 to your computer and use it in GitHub Desktop.
backbone model attr transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//IONUX.Models.UserRegistrationModel = Backbone.Model.extend({ | |
IONUX.Models.UserRegistrationModel = IONUX.Models.EditableResource.extend({ | |
url: "/userprofile/", | |
idAttribute: "_id", | |
schema: { | |
name: 'Text', | |
description: 'Text', | |
'_contact': { | |
type: 'Object', | |
title: false, | |
subSchema: { | |
organization_name: { type: 'Text', title: 'Organization Name' }, | |
position_name: { type: 'Text', title: 'Position' }, | |
email: { type: 'Text', title: 'Email', dataType: 'email' }, | |
street_address: { type: 'Text', title: 'Street Address' }, | |
city: { type: 'Text', title: 'City' }, | |
postal_code: { type: 'Text', title: 'Postal Code' }, | |
country: { type: 'Text', title: 'Country' }, | |
url: { type: 'Text', title: 'Contact URL' }, | |
//phone: 'Text', // @TODO: how | |
}, | |
}, | |
'_variables': { | |
type: 'Object', | |
title: false, | |
subSchema: { | |
notification_contact: { type: 'Radio', title: 'Notification Method', options: ['Email', 'SMS'] }, | |
sms_email: { type: 'Checkbox', title: "Use email->SMS" }, | |
ooi_system_change: { type: 'Checkbox', title: "OOI system change information" }, | |
ooi_project_updates: { type: 'Checkbox', title: "OOI project updates" }, | |
ocean_leadership_news: { type: 'Checkbox', title: "Ocean Leadership News" }, | |
ux_improvement_program: { type: 'Checkbox', title: "Participate in OOI User Experience Improvement Program" }, | |
} | |
}, | |
}, | |
attr_convert: { | |
'variables': { | |
parse: function(v) { | |
/** | |
* Input: array of { name: <key>, value: <value> } literal objects | |
* Output: object with keys -> values | |
*/ | |
return _.object(_.map(v, function(vv) { return [vv.name, vv.value]; } )); | |
}, | |
serialize: function(v) { | |
/** | |
* Input: object with keys -> values | |
* Output: array of { name: <key>, value: <value> } literal objects | |
*/ | |
return _.map(v, function(vv, kk) { return { name: kk, value: vv }; }); | |
}, | |
}, | |
}, | |
attr_convert_serialize: function(attrs) { | |
/** | |
* Call this from your toJSON. | |
*/ | |
attrs = _.clone(attrs); | |
_.each(this.attr_convert, function(v, k) { | |
if (_.has(attrs, k)) { | |
attrs[k] = v.serialize(attrs[k]); | |
} | |
}); | |
return attrs; | |
}, | |
attr_convert_parse: function(data) { | |
/** | |
* Call this from your parse method. | |
*/ | |
data = _.clone(data); | |
_.each(this.attr_convert, function(v, k) { | |
if (_.has(data, k)) { | |
data[k] = v.parse(data[k]); | |
} | |
}); | |
return data; | |
}, | |
toJSON: function() { | |
var o = IONUX.Models.EditableResource.prototype.toJSON.call(this); | |
o = this.attr_convert_serialize(o); | |
// remove shadow vars | |
delete o._variables; | |
delete o._contact; | |
return o; | |
}, | |
parse: function(resp) { | |
var data = resp.data; | |
data = this.attr_convert_parse(data); | |
// clone into shadow properties for backbone forms editing purposes only | |
data._variables = _.clone(data.variables); | |
data._contact = _.clone(data.contact); | |
// set up changed reactions, so we propogate changes back to the real properties | |
this.on("change:_variables", this.changedVariables); | |
this.on("change:_contact", this.changedContact); | |
return data; | |
}, | |
changedVariables: function(ev) { | |
var co = _.clone(this.get('variables') || {}); | |
_.extend(co, this.get('_variables')); | |
this.set('variables', co); | |
}, | |
changedContact: function(ev) { | |
var co = _.clone(this.get('contact') || {}); | |
_.extend(co, this.get('_contact')); | |
this.set('contact', co); | |
}, | |
destroy: function() { | |
this.off('change:_variables', this.changedVariables); | |
this.off('change:_contact', this.changedContact); | |
}, | |
merge: function(attribute, value) { | |
/** | |
* Instead of overwriting full objects, this method merges the passed in value | |
* on top of the current value. | |
*/ | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment