Ember-成功保存后将属性设置为默认状态

时间:2014-08-06 09:33:35

标签: ember.js

我有一个很长的表单,包含10个以上动态填充的表单

这些是一些属性

  metadata : {},
  creators : [],
  subjects : [],
  contributors : [],
  dates : [],
  identifiers : [],
  descriptions : [],
  geolocations : [],
  alternateidentifiers : [],
  sizes : [],
  formats : [],
  rights : [],
  files : [],
  newMetaDataRecord : null,

  // Choices from backend
  resourceTypesGeneral : null,
  contributorsType : null,
  dateType : null,
  relatedIdentifTypes : null,
  relationType : null,
  descriptionType : null,

  //BACKEND errorLOG array
  errorMessageObject : [],

  // Validations
  invalidCreatorsList : [],
  validCreators : false,

  invalidSubjectsList : [],
  validSubjects : true,

  invalidContributorsList : [],
  validContributors : true,

  invalidDatesList : [],
  validDates : true,

  invalidIdentifiersList : [],
  validIdentifiers : true,

  invalidDescriptionsList : [],
  validDescriptions : true,

  invalidGeosList : [],
  validGeos : true,

  invalidalternateIdentifiersList : [],
  validAlternates : true,

  invalidSizesList : [],
  validSizes : true,

  invalidFormatsList : [],
  validFormats : true,

  invalidRightsList : [],
  validRights : true,

我曾经有一个名为

的函数
setFormToDefaultState : function(){
    this.set('metadata', this.createEmptyMetaData());
    this.set('metadata.resource_type', this.createEmptyResourceType());
    this.createEmptyCreator();
  },

类似于Initialize

init : function(){
    this._super();

    this.set('metadata', this.createEmptyMetaData());
    this.set('metadata.resource_type', this.createEmptyResourceType());
    this.createEmptyCreator();
  },

当我拥有较少的属性时,这种方式确实有效,但现在有近30个属性设置默认值,我想知道是否有更简单的解决方案。在路由中执行它也不起作用,因为在reNavigation字段中将丢失(已填充数据)。

还有一个函数reset.all()?

当前解决方案

var arProperties = ["creators", "subjects", "contributors", "dates", "identifiers", "descriptions", "geolocations", "alternateidentifiers", "sizes", "formats", "rights", "files", "titles", "taxons", "invalidTaxonsList", "invalidCreatorsList", "invalidSubjectsList", "invalidContributorsList", "invalidDatesList", "invalidIdentifiersList", "invalidDescriptionsList", "invalidGeolocationsList", "invalidAlternateidentifiersList", "invalidSizesList", "invalidFormatsList", "invalidRightsList", "invalidTitlesList"];
    arProperties.forEach(function(arProperty) { 
      self.set(arProperty, []); 
    });
    var trueProperties  = ["formInvalid", "validTaxons", "validSubjects", "validContributors", "validDates", "validIdentifiers", "validDescriptions", "validGeolocations", "validAlternateidentifiers", "validSizes", "validFormats", "validRights"];
    trueProperties.forEach(function(trueProperty){
      self.set(trueProperty, true);
    });

1 个答案:

答案 0 :(得分:0)

我通过在路线上设置属性来实现这一点。因此,如果您第一次输入路线,则会加载模型,然后属性会更改,并在第二次访问时路由"返回"被叫。这样您的输入保持不变,您仍然可以轻松地重置所有属性。

import RouteMixin from 'appkit/misc/routemixin';
export default Ember.Route.extend(Ember.SimpleAuth.AuthenticatedRouteMixin, RouteMixin, {
  model : function(){
  var self = this;

    if(Ember.isNone(self.get('controller.routeHasBeenLoaded'))){
         return Ember.RSVP.hash({

            invalidTaxonsList : [],
            validTaxons : true,

            invalidCreatorsList : [],
            validCreators : false,

            invalidSubjectsList : [],
            validSubjects : true,

            invalidContributorsList : [],
            validContributors : true,

            invalidDatesList : [],
            validDates : true,

          });
    } else {
        return;
    } 
  },
 }

});

控制器

init : function(){
 this._super();
 this.set('routeHasBeenLoaded', true)
}