简体   繁体   中英

what is the best way to enable and disable save button in Aurelia with complex domain object?

I have editMessage.js constructor as:

 constructor(){

  var messageStringProperty1 = new messageStringProperty();
  messageStringProperty1.propertyName = 'title';
  messageStringProperty1.propertyValue = 'This is the menu for school campus';
  this.messageProperties[0] = messageStringProperty1;

  var messageIntegerProperty1 = new messageIntegerProperty();
  messageIntegerProperty1.propertyName = 'Menu Title Font Size';
  messageIntegerProperty1.selectedValue = 30;
  messageIntegerProperty1.selectableValues = [10, 12, 14, 30]
  this.messageProperties[1] = messageIntegerProperty1;

  var messageImageProperty1 = new messageImageProperty();
  messageImageProperty1.propertyName = 'Background Image';
  messageImageProperty1.elementName = 'BackgroundImage';
  messageImageProperty1.originalImage = "http://i2.wp.com/ejohn.org/files/Timers.png";
  this.messageProperties[2] = messageImageProperty1;

 var messageColorProperty1 = new messageColorProperty();
  messageColorProperty1.propertyName = 'Title Color';
  messageColorProperty1.propertyValue = '#ffffff';
  messageColorProperty1.elementName = 'TitleColor';
  this.messageProperties[3] = messageColorProperty1;

}

and editMessage.html (view) is:

<li class="list-group-item" repeat.for="p of messageProperties">
   <div if.bind="p.propertyType == 'string'">
    <div class="form-group">
       <label for="ln1">Name: ${p.propertyName}</label>
       <input type="text" value.bind="p.propertyValue" class="form-control" id="ln1" >
    </div>
  </div>
  <div if.bind="p.propertyType == 'integer'">
    <div class="form-group">
       <label for="ln2">Name: ${p.propertyName}</label>
       <input type="text" value.bind="p.selectedValue" class="form-control" id="ln2" >
      <select-picker selectable-values.bind="p.selectableValues" selected-value.two-way="p.selectedValue"></select-picker>
   </div>
  </div>
  <div if.bind="p.propertyType == 'image'">
    <div class="form-group, message-border">
      <div class="message-property-name">
       Name: ${p.propertyName}
       <input type="text" value.bind="p.propertyValue" id="ln3" >
      </div>
      <image-picker selected-file.two-way="p.selectedFile" original-image.two-way="p.originalImage" element-name.bind="p.elementName"></image-picker>
    </div>    
  </div>
  <div if.bind="p.propertyType == 'color'">
    <div class="form-group">
       <label for="ln3">Name: ${p.propertyName}</label>
       <input type="text" value.bind="p.propertyValue" class="form-control" id="ln3" >
       <color-picker element-name.bind="p.elementName" initial-color.bind="p.propertyValue"></color-picker>
    </div>    
  </div>
</li>   

I would like to have a save button so that if any of my message*property objects change it enables otherwise it stays disabled.

In the past I have created a timer and done some dirty checking by comparing original values with the changed values. What is the best approach with Aurelia to do this?

Thanks to zewa66, I copied the same methodology as github.com/aurelia/app-contacts . What I did was on each business object message*Property I created a hasPropertyChanged method.

For example on messageStringProperty I have:

get propertyHasChanged(){
  return this.originalValue != this.propertyValue;
} 

Then on the main view I have:

 get canSave(){
   for (var i=0; i< this.messageProperties.length; i++){
     if (this.messageProperties[i].propertyHasChanged){
      return true;
    }
  }

  return false;
} 

and on the view we have:

 <button class="btn btn-success" click.trigger="saveChanges()"
                          disabled.bind="!canSave">Save Changes</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM