Created
May 20, 2015 03:58
-
-
Save elchele/6afc297eddf20ef147cf to your computer and use it in GitHub Desktop.
Custom record view controller for setting entire record to read-only conditionally
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
({ | |
/* Author: Angel Magaña -- cheleguanaco@cheleguanaco.com | |
* File: ./custom/modules/Opportunities/clients/base/views/record/record.js | |
* | |
* Set all fields to read-only conditionally | |
*/ | |
extendsFrom: 'RecordView', | |
_renderHtml: function(){ | |
//Check the current status of selected opportunity record | |
var myCriteria = this.model.get('sales_status'); | |
//If status is 'Closed Won' do no allow any edits, by setting all fields to read-only | |
if (myCriteria === 'Closed Won') | |
{ | |
var self = this; | |
_.each(this.model.fields, function(field) { | |
//Add field to the array defining read-only fields | |
self.noEditFields.push(field.name); | |
}); | |
} | |
this._super('_renderHtml'); | |
}, | |
_dispose: function(){ | |
this._super('_dispose'); | |
} | |
}) |
Hi Angel,
Did you ever find a way to remove the extra buttons from the page? I've found that using jquery to hide the save/cancel buttons works if I want the entire record to be non-editable. When I'm trying to only make one field non-editable then hiding the buttons does not work because clicking the edit button will not pre-populate those buttons.
Any help is appreciated.
Thanks!
- Daniel
Hi Daniel,
I found a solution, this is what works for me now:
Hope this helps,
initialize: function (options) {
this._super('initialize', [options]);
this.model.on('sync', _.bind(this._readonlyFields, this)); //make readonly fields when data gets synced
this._readonlyFields();
this.events["change input[name=contract_signer_c]"] = '_readonlyFields'; //trigger readonly fields when user changes the field
},
_renderHtml: function() {
var self = this;
self._super('_renderHtml');
},
_readonlyFields: function() {
var self = this;
var status_oppty_c = self.model.get('status_oppty_c');
_.each(self.model.fields, function(field) {
switch(field.name){
case "opportunity_number_c": //make these fields readonly
case "account_name":
self.noEditFields.push(field.name);
$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
break;
default: //keep all other fields editable
break;
}
});
if (status_oppty_c == "Approved"){
_.each(self.model.fields, function(field) {
switch(field.name){
case "contract_signer_c": //keep all these fields editable when status is Approved
case "order_type_c":
case "send_contract_c":
case "counter_signed_date_c":
case "customer_signed_date_c":
case "previouscontractdate_c":
case "specialterms_c":
case "new_term_c":
case "date_closed":
break;
default: //make all other fields NON editable mode
self.noEditFields.push(field.name);
$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
}
});
}
},
_dispose: function() {
this._super('_dispose', []);
}
Regards,
Angel Martinez
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi andrewgin,
I'm facing a similar issue, I had .js files running just fine on versions before 7.8.
This code solves some part of it in 7.8:
One thing I'm struggling with right now is, that if I use the last two render functions, the fields go to read-only mode, but the Save, Edit and Cancel buttons are visible on the page. But in the other hand, if I don't use any of those functions, the fields won't be read-only mode.
I'm not a JavaScript expert but let me know if that helps, or if you already found a solution to your issue. ;)
Regards,
Angel Mtz.