How set readOnly dynamically in record.js?

I'm need set readOnly in custom field when logged user not admin in record.js

if(app.user.get('type') == 'admin')
  //set readOnly field
else
  //set editable fiel

Parents
  • You should be able to adapt this Gist to do what you want:

    Custom record view controller for setting entire record to read-only conditionally · GitHub

    They key is to add the field name into the "noEditFields" array.

  • Hi
    This code works for me in 7.8 when I pause it at breakpoints. However it appears that this javascript is running before the model has been loaded, hence I get
     this.model.attributes.sales_status is undefined
    When I run it without breakpoints.

    When I insert a breakpoint and type in the javascript console
    this.model.attributes.sales_status
    It comes up with "Closed Won", but when I have
    console.log(this.model.attributes.sales_status)
    it just prints undefined.
    Is there a way to make sure this only runs once the model has been fully loaded?

    Also is it preferred to set fields to readonly using this method or sugarlogic dependencies?

  • Hi,

    Try 

     this.$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
    instead of
    self.noEditFields.push(field.name); As this will not work on 7.8.. versions.
    Also disable edit and save action.
    $('.headerpane a[name=edit_button]').addClass("disabled");
    $('.headerpane a[name=save_button]').addClass("disabled");
    var self = this;
    _.each(this.model.fields, function(field) {
        //self.noEditFields.push(field.name);
        this.$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
    },this);

    Regards,

    Usman

  • Thanks Usman, 

    I found out that your solution is also a good way to do it.

    I've got my code working by doing the following:

    ({
       extendsFrom: 'RecordView',
       initialize: function (options) {
          this._super('initialize', [options]);
          this.model.on('sync', _.bind(this._readonlyFields, this));
          this._readonlyFields();
       },

       _renderHtml: function() { //No OPTIONS parameter
          var self = this;
          self._super('_renderHtml');
       },

       _readonlyFields: function() {
          var self = this;

          _.each(self.model.fields, function(field) { //date_closed
             self.noEditFields.push(field.name);
             $('.record-edit-link-wrapper[data-name=' + field.name + ']').remove(); //remove the little pencil from the field
          });
       },

       _dispose: function() {
          this._super('_dispose', []);
       }
    })

Reply
  • Thanks Usman, 

    I found out that your solution is also a good way to do it.

    I've got my code working by doing the following:

    ({
       extendsFrom: 'RecordView',
       initialize: function (options) {
          this._super('initialize', [options]);
          this.model.on('sync', _.bind(this._readonlyFields, this));
          this._readonlyFields();
       },

       _renderHtml: function() { //No OPTIONS parameter
          var self = this;
          self._super('_renderHtml');
       },

       _readonlyFields: function() {
          var self = this;

          _.each(self.model.fields, function(field) { //date_closed
             self.noEditFields.push(field.name);
             $('.record-edit-link-wrapper[data-name=' + field.name + ']').remove(); //remove the little pencil from the field
          });
       },

       _dispose: function() {
          this._super('_dispose', []);
       }
    })

Children
No Data