Created
October 20, 2011 13:51
-
-
Save fxposter/1301190 to your computer and use it in GitHub Desktop.
DO NOT EVER DO THIS AT HOME
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
Application.Views.Network.RelationControl = (function () { | |
var view = { events: {} }; | |
var toggleMap = [ | |
[['accept_profile', 'decline_profile'], ['friendship_request_block']], | |
[[{ post: 'friend_profile' }, { ajaxDelete: 'unfriend_profile' }], ['trust_block', 'distrust_block']], | |
[[{ post: 'subscribe_profile' }, { ajaxDelete: 'unsubscribe_profile' }], ['subscribe_block', 'unsubscribe_block']], | |
[['accept_employment', 'decline_employment'], ['employment_request_block']], | |
[[{'post':'create_employment'}, {'ajaxDelete':'remove_employment'}], ['employment_block']], | |
[['promote_to', 'demote_from'], ['employment_role_block']] | |
]; | |
var _createHandler = function (togglables, method) { | |
return function (e) { | |
$[method](e.target.href, function () { | |
_.map(togglables, function (cl) { | |
this.$(e.currentTarget).closest('ul').find('.'+cl).toggle(); | |
}); | |
}); | |
e.preventDefault(); | |
}; | |
}; | |
_.map(toggleMap, function (pair) { | |
var classes = pair[0], togglables = pair[1]; | |
_.map(classes, function (htmlClass) { | |
var httpVerb; | |
if (!_.isString(htmlClass)) { | |
httpVerb = _.keys(htmlClass)[0]; | |
htmlClass = _.values(htmlClass)[0]; | |
} else { | |
httpVerb = 'get'; | |
} | |
var methName = htmlClass; // camelCase it | |
view[methName] = _createHandler(togglables, httpVerb); | |
view.events['click .'+htmlClass] = methName; | |
}); | |
}); | |
return Backbone.View.extend(view); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment