Created
June 22, 2015 15:34
-
-
Save blvp/5a72b34f1fe4b0e7afa3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var SubscriberServicesPage = extend.Page({ | |
| delegatedEvents: { | |
| "click [id^='js_#service_id_']": "manageService", | |
| "click #js_save_services_changes": "saveChanges" | |
| }, | |
| initializeServicesList: function () { | |
| var that = this; | |
| $("[id^='" + this.jsServiceRowIdPrefix + "']").each(function () { | |
| var $serviceRow = $(this); | |
| var serviceId = $serviceRow.data('id'); | |
| that.servicesStatus.push(new ServiceStatus({ | |
| serviceId: serviceId, | |
| enabled: $serviceRow.hasClass('js-active') | |
| })); | |
| }) | |
| }, | |
| manageService: function (evt) { | |
| var serviceId = evt.$el.data('id'); | |
| var foundServiceStatus = _.findWhere(this.servicesStatus, {serviceId: serviceId}); | |
| var index = this.servicesStatus.indexOf(foundServiceStatus); | |
| this.servicesStatus[index].changed = !evt.$el.data('enabled') == evt.$el.is(':checked'); | |
| this.changeSaveButtonStatus(); | |
| }, | |
| changeSaveButtonStatus: function () { | |
| var isAnyChanged = _.some(this.servicesStatus, function (item) { | |
| return item.changed; | |
| }); | |
| if (isAnyChanged) { | |
| $(this.jsSaveChangesButtonSelector).removeClass(this.jsSaveChangesButtonInactiveClass); | |
| } else { | |
| $(this.jsSaveChangesButtonSelector).addClass(this.jsSaveChangesButtonInactiveClass); | |
| } | |
| }, | |
| saveChanges: function (evt) { | |
| $.ajax({ | |
| url: "/services/manageAll", | |
| data: JSON.stringify({ | |
| serviceIds: _.filter(evt.that.servicesStatus, function (item) { | |
| return item.changed == true; | |
| }) | |
| }), | |
| method: 'post', | |
| contentType: 'application/json' | |
| }).success(function (data) { | |
| window.location = "/services/main" | |
| }).error(function () { | |
| console.log("something happened while saving changes at services page") | |
| }) | |
| } | |
| }); | |
| var ServiceStatus = function (options) { | |
| return { | |
| "serviceId": options.serviceId, | |
| /** | |
| * true means enabled; false not | |
| */ | |
| "enabled": options.enabled, | |
| /** | |
| * by default it is false | |
| */ | |
| "changed": options.changed || false | |
| } | |
| }; | |
| $(document).ready(function () { | |
| window.subscriberPage = new SubscriberServicesPage({ | |
| jsRowActiveClasses: [ | |
| "active", | |
| "js-active" | |
| ], | |
| jsServiceRowIdPrefix: "js-service-row_id_", | |
| jsServiceCheckBoxIdPrefix: "js_#service_id_", | |
| jsSaveChangesButtonSelector: "#js_save_services_changes", | |
| jsSaveChangesButtonInactiveClass: "inactive", | |
| servicesStatus: [] | |
| }); | |
| subscriberPage.initialize(); | |
| subscriberPage.initializeServicesList(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment