Skip to content

Instantly share code, notes, and snippets.

@elchele
Created May 20, 2015 03:58
Show Gist options
  • Save elchele/6afc297eddf20ef147cf to your computer and use it in GitHub Desktop.
Save elchele/6afc297eddf20ef147cf to your computer and use it in GitHub Desktop.
Custom record view controller for setting entire record to read-only conditionally
({
/* 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');
}
})
@dmoser49
Copy link

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

@angel-mtz
Copy link

angel-mtz commented Nov 9, 2017

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