Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript observabel数组中添加的元素不可观测+;反应_Javascript_Reactjs_Mobx_React Dom_Mobx React - Fatal编程技术网

Javascript observabel数组中添加的元素不可观测+;反应

Javascript observabel数组中添加的元素不可观测+;反应,javascript,reactjs,mobx,react-dom,mobx-react,Javascript,Reactjs,Mobx,React Dom,Mobx React,我有一个可观察的阵列,它工作得很好。我有一个添加按钮,用于将元素添加到这个数组中。在将元素添加到这个数组中之后,添加的元素是不可见的,但其他元素仍然是可见的。 我正在使用mobx react。 这是代码片段: import * as React from "react"; import {computed, observable} from "mobx"; import {observer} from "mobx-react"; import {AreaBody} from "../Ge

我有一个可观察的阵列,它工作得很好。我有一个添加按钮,用于将元素添加到这个数组中。在将元素添加到这个数组中之后,添加的元素是不可见的,但其他元素仍然是可见的。 我正在使用mobx react。 这是代码片段:

    import * as React from "react";
import {computed, observable} from "mobx";
import {observer} from "mobx-react";
import {AreaBody} from "../General/AreaBody";
import {AppStore} from "../Stores/AppStore";
import {Strings} from "../Stores/StringsStore";
import {DialogStore, DialogResult} from "../Stores/DialogStore";
import {NotificationStore} from "../Stores/NotificationStore";
import {AddButton} from "../Buttons/AddButton";
import {SaveButton} from "../Buttons/SaveButton";
import {DeleteButton} from "../Buttons/DeleteButton";
import {CloseButton} from "../Buttons/CloseButton";
import {Toolbar} from "../General/Toolbar";
import {InputBooleanComponent} from "../Inputs/InputBooleanComponent";
import {InputNumberComponent} from "../Inputs/InputNumberComponent";
import {InputChoiseComponent} from "../Inputs/InputChoiseComponent";
import {InputTextComponent} from "../Inputs/InputTextComponent";
import * as MetaManagement from "../MetaManagement";
import * as Utilities from "../Utilities";
import {Urls} from "../Urls";
import * as UiStore from "../States/AreaUiInfo";

interface IMetaProps extends UiStore.IAreaUiInfo {
    params: IMetaPropsParams;
}

interface IMetaPropsParams {
    id: string;
}

@observer
export class MetaEdit extends React.Component<IMetaProps, {}> {
    @observable model = new MetaManagement.MetaDetails();
    @observable hash: string;
    @observable isBusy: boolean = true;
    @observable canSaveData: boolean = false;

    componentDidMount() {
        window.addEventListener('beforeunload', this.keepOnPage);
        this.getModel();
        this.canSaveData = this.canSaveModel();
    }

    componentWillUnmount() {
        window.removeEventListener('beforeunload', this.keepOnPage);
    }


    keepOnPage = (e: any) => {
        if (this.canSaveModel()) {
            DialogStore.show("do you want to reload this site?", 'Warning!\n\nNavigating away from this page will delete your text if you haven\'t already saved it.').then((result: DialogResult) => {
                if (result == DialogResult.Yes) {
                    return 'Warning!\n\nNavigating away from this page will delete your text if you haven\'t already saved it.';
                }
            });
        }
    }

    componentDidUpdate(prevProps: IMetaProps) {
        if (this.props.params.id != this.model.guid) {
            this.getModel();
            this.canSaveData = this.canSaveModel();
        }
    }

    getModel() {
        this.isBusy = true;
        MetaManagement.getMetaAsync(this.props.params.id).then(model => {
            this.model = this.fillMissing(model);
            this.hash = JSON.stringify(this.model);
            this.forceUpdate();
            NotificationStore.clearMessage();
            this.isBusy = false;
        }).catch(error => {
            NotificationStore.showErrorMessage(error);
        });
    }

    //@computed get canSave(): boolean {
    //  for (let i = 0; i < this.model.metaValues.length; i++) {
    //      if (!this.model.metaValues[i].value.isValid || !this.model.metaValues[i].value.value) {
    //          return false;
    //      }
    //  }
    //  return this.model.id.isValid && this.model.caption.isValid && (this.hash != JSON.stringify(this.model));
    //}

    canSaveModel = () => {
        for (let i = 0; i < this.model.metaValues.length; i++) {
            if (!this.model.metaValues[i].value.isValid || !this.model.metaValues[i].value.value) {
                return false;
            }
        }
        return this.model.id.isValid && this.model.caption.isValid && (this.hash != JSON.stringify(this.model));
    }

    handleSave = () => {
        if (this.canSaveModel()) {
            let updateModel = new MetaManagement.MetaDetailsUpdate();
            updateModel.lastModified = this.model.lastModified;
            updateModel.captionLastModified = this.model.captionLastModified;
            updateModel.guid = this.model.guid;
            updateModel.dataType = this.model.dataType.value;
            updateModel.id = this.model.id.value;
            updateModel.caption = this.model.caption.value;
            updateModel.required = this.model.required.value;
            updateModel.showAsColumn = this.model.showAsColumn.value;
            updateModel.showAsFilter = this.model.showAsFilter.value;
            updateModel.filterMaxValues = this.model.filterMaxValues.value;
            updateModel.regex = this.model.regex.value;
            updateModel.multiple = this.model.multiple.value;
            updateModel.languageDependent = this.model.languageDependent.value;
            updateModel.predefined = this.model.predefined.value;
            if (updateModel.predefined) {
                updateModel.metaValues = this.model.metaValues;
            }
            else {
                updateModel.metaValues = [];
            }

            MetaManagement.insertOrUpdateMetaAsync(updateModel).then(model => {
                this.model = this.fillMissing(model);
                if (this.model.isValid) {
                    this.hash = JSON.stringify(this.model);
                    NotificationStore.showSuccessMessage(Strings.general.saveSuccessMessage);
                }
                else {
                    NotificationStore.showErrorMessage(model.validationMessage);
                }
            }).catch(error => {
                NotificationStore.showErrorMessage(error);
            });
        }
        this.canSaveData = this.canSaveModel();
    }

    handleDelete = () => {
        DialogStore.show(Utilities.format(Strings.general.dialogDeleteTitle, this.model.caption.value), Utilities.format(Strings.general.dialogDeleteText, this.model.caption.value)).then(result => {
            if (result == DialogResult.Yes) {
                MetaManagement.removeMetaAsync({ guid: this.model.guid, lastModified: null }).then(status => {
                    NotificationStore.showSuccessMessage(Strings.general.deleteSuccessMessage);
                    AppStore.history.replace(Urls.metas);
                }).catch(error => {
                    NotificationStore.showErrorMessage(error);
                });
            }
        });
    }

    handleCancel = () => {
        AppStore.history.replace(Urls.metas);
    }

    handleChange = () => {
        this.canSaveData = this.canSaveModel();
        NotificationStore.clearMessage();
    }

    @computed get showRegex(): boolean {
        return this.model.dataType.value == "Text" || this.model.dataType.value == "Hierarchy";
    }

    @computed get showLanguageDependent(): boolean {
        return this.model.dataType.value == "Text" || this.model.dataType.value == "Hierarchy";
    }

    @computed get showMultiple(): boolean {
        return this.model.dataType.value != "Boolean";
    }

    @computed get showPredifined(): boolean {
        return this.model.dataType.value == "Text" || this.model.dataType.value == "Number" || this.model.dataType.value == "Hierarchy" || this.model.dataType.value == "DocumentLink";
    }

    fillMissing(model: MetaManagement.MetaDetails) {
        model.dataType.label = Strings.metaManagement.metaEdit.dataType;
        model.id.label = Strings.metaManagement.metaEdit.id;
        model.id.placeHolder = Strings.metaManagement.metaEdit.idPlaceHolder;
        model.caption.label = Strings.metaManagement.metaEdit.caption;
        model.caption.placeHolder = Strings.metaManagement.metaEdit.captionPlaceHolder;
        model.regex.label = Strings.metaManagement.metaEdit.regex;
        model.regex.placeHolder = Strings.metaManagement.metaEdit.regexPlaceHolder;
        model.predefined.label = Strings.metaManagement.metaEdit.preDefined;
        model.multiple.label = Strings.metaManagement.metaEdit.multiple;
        model.required.label = Strings.metaManagement.metaEdit.required;
        model.languageDependent.label = Strings.metaManagement.metaEdit.languageDependent;
        model.showAsColumn.label = Strings.metaManagement.metaEdit.showAsColumn;
        model.showAsFilter.label = Strings.metaManagement.metaEdit.showAsFilter;
        model.filterMaxValues.label = Strings.metaManagement.metaEdit.filterMaxValues;
        model.filterMaxValues.placeHolder = Strings.metaManagement.metaEdit.filterMaxValuesPlaceHolder;
        for (let i = 0; i < model.metaValues.length; i++) {
            model.metaValues[i].value.regex = "^[a-zA-Z0-9- _]+$";
        }
        return model;
    }

    render() {
        let regex = this.showRegex ? <InputTextComponent model={this.model.regex} onChange={this.handleChange} /> : null;
        let languageDependent = this.showLanguageDependent ? <InputBooleanComponent model={this.model.languageDependent} onChange={this.handleChange} /> : null;
        let multiple = this.showMultiple ? <InputBooleanComponent model={this.model.multiple} onChange={this.handleChange} /> : null;
        let predefined = this.showPredifined ? <InputBooleanComponent model={this.model.predefined} onChange={this.handleChange} /> : null;
        let values = this.showPredifined && this.model.predefined.value ? <div><h2>{Strings.metaManagement.metaEdit.preDefinedValues}</h2><MetaValuesEdit handleChange={this.handleChange} values={this.model.metaValues} readonly={this.model.predefined.isReadOnly} number={this.model.dataType.value == "Number"} /></div> : null;

        return <AreaBody loading={this.isBusy} areaUiInfo={this.props.areaUiInfo}>
            <Toolbar>
                <SaveButton onClick={this.handleSave} disabled={!this.canSaveData} label={Strings.general.saveButtonLabel}/>
                <DeleteButton onClick={this.handleDelete} label={Strings.general.deleteButtonLabel}/>
                <span className="buttongroup is--buttongroup-right">
                    <CloseButton onClick={this.handleCancel} />
                </span>
            </Toolbar>
            <div className="area-grid__body-content">
                <div className="content-main">
                    <h1>{Strings.metaManagement.metaEdit.title}</h1>
                    <div>
                        <InputChoiseComponent model={this.model.dataType} onChange={this.handleChange} />
                        <InputTextComponent model={this.model.id} onChange={this.handleChange} />
                        <InputTextComponent model={this.model.caption} onChange={this.handleChange} />
                        <InputBooleanComponent model={this.model.required} onChange={this.handleChange} />
                        <InputBooleanComponent model={this.model.showAsColumn} onChange={this.handleChange} />
                        <InputBooleanComponent model={this.model.showAsFilter} onChange={this.handleChange} />
                        <InputNumberComponent model={this.model.filterMaxValues} locale={navigator.language} onChange={this.handleChange}/>
                        {regex}
                        {multiple}
                        {languageDependent}
                        {predefined}
                        {values}
                    </div>
                </div>
            </div>
        </AreaBody>;
    }
}

interface IMetaValuesProps {
    values: MetaManagement.MetaValue[];
    readonly: boolean;
    number: boolean;
    handleChange: () => void;
}

@observer
class MetaValuesEdit extends React.Component<IMetaValuesProps, {}> {


    handleAdd = () => {
        let newValue = new MetaManagement.MetaValue();
        newValue.guid = Utilities.newGuid();
        newValue.canDelete = true;
        newValue.value.isReadOnly = false;
        newValue.value.isValid = true;
        newValue.value.value = "";
        newValue.value.regex = "^[a-zA-Z0-9- _]+$";
        this.props.values.push(newValue);
        this.props.handleChange();
    }

    handleDelete = (item: MetaManagement.MetaValue) => {
        for (let i = 0; i < this.props.values.length; i++) {
            if (this.props.values[i].guid == item.guid) {
                this.props.values.splice(i, 1);
                this.props.handleChange();
                return;
            }
        }
    }

    handleChange = () => {
        this.props.handleChange();
    }

    render() {
        return <div className="list-object is--tools-single">
            <div className="toolbar">
                <span className="buttongroup is--buttongroup-right">
                    <AddButton onClick={this.handleAdd} disabled={this.props.readonly} />
                </span>
            </div>
            <ul>
                {
                    this.props.values.map(item => {
                        return <li key={item.guid}>
                            <div>
                                <div className="list-object__tools">
                                    <div className="tinytool">
                                        <DeleteButton onClick={() => this.handleDelete(item) } disabled={!item.canDelete} />
                                    </div>
                                </div>
                                <div className="list-object__name">
                                    <InputTextComponent onChange={this.handleChange} model={item.value} />
                                </div>
                            </div>
                        </li>
                    })
                }
            </ul>
        </div>;
    }
}
import*as React from“React”;
从“mobx”导入{computed,observable};
从“mobx react”导入{observer};
从“./常规/AreaBody”导入{AreaBody}”;
从“./Stores/AppStore”导入{AppStore};
从“./Stores/StringsStore”导入{Strings};
从“./Stores/DialogStore”导入{DialogStore,DialogResult};
从“./Stores/NotificationStore”导入{NotificationStore}”;
从“./Buttons/AddButton”导入{AddButton};
从“./Buttons/SaveButton”导入{SaveButton};
从“./按钮/DeleteButton”导入{DeleteButton};
从“./Buttons/CloseButton”导入{CloseButton};
从“./General/Toolbar”导入{Toolbar};
从“./Inputs/InputBooleanComponent”导入{InputBooleanComponent};
从“./Inputs/InputNumberComponent”导入{InputNumberComponent};
从“./Inputs/inputChoiceComponent”导入{inputChoiceComponent};
从“./Inputs/inputExtComponent”导入{inputExtComponent};
从“./MetaManagement”导入*作为元管理;
从“./实用程序”导入*作为实用程序;
从“./URL”导入{URL};
从“./States/AreaUiInfo”导入*作为UI存储;
接口IMetaProps扩展了UiStore.IAreaUiInfo{
参数:IMetaPropsParams;
}
APROPSPARAMS接口{
id:字符串;
}
@观察者
导出类MetaEdit扩展了React.Component{
@可观察模型=新的元管理。元细节();
@可观察散列:字符串;
@可观察isBusy:布尔值=真;
@可观察的canSaveData:布尔值=false;
componentDidMount(){
window.addEventListener('beforeunload',this.keepOnPage);
这个.getModel();
this.canSaveData=this.canSaveModel();
}
组件将卸载(){
window.removeEventListener('beforeunload',this.keepOnPage);
}
keepOnPage=(e:any)=>{
if(this.canSaveModel()){
DialogStore.show(“是否要重新加载此网站?”,“警告!\n\n如果您尚未保存文本,则离开此页面将删除它。)。然后((结果:DialogResult)=>{
if(result==DialogResult.Yes){
return“警告!\n\n如果您尚未保存文本,则离开此页面将删除该文本。”;
}
});
}
}
componentDidUpdate(prevProps:IMetaProps){
if(this.props.params.id!=this.model.guid){
这个.getModel();
this.canSaveData=this.canSaveModel();
}
}
getModel(){
this.isBusy=true;
getMetaAsync(this.props.params.id)。然后(model=>{
this.model=this.fillMissing(model);
this.hash=JSON.stringify(this.model);
这个.forceUpdate();
NotificationStore.clearMessage();
this.isBusy=false;
}).catch(错误=>{
NotificationStore.showErrorMessage(错误);
});
}
//@计算的get canSave():布尔值{
//for(设i=0;i{
for(设i=0;i{
if(this.canSaveModel()){
让updateModel=new MetaManagement.MetaDetailsUpdate();
updateModel.lastModified=this.model.lastModified;
updateModel.captionLastModified=this.model.captionLastModified;
updateModel.guid=this.model.guid;
updateModel.dataType=this.model.dataType.value;
updateModel.id=this.model.id.value;
updateModel.caption=this.model.caption.value;
updateModel.required=this.model.required.value;
updateModel.showAsColumn=this.model.showAsColumn.value;
updateModel.showAsFilter=this.model.showAsFilter.value;
updateModel.filterMaxValues=this.model.filterMaxValues.value;
updateModel.regex=this.model.regex.value;
updateModel.multiple=this.model.multiple.value;
updateModel.languageDependent=this.model.languageDependent.value;
updateModel.prefined=this.model.prefined.value;
if(updateModel.预定义){
updateModel.metaValues=this.model.metaValues;
}
否则{
updateModel.metaValues=[];
}
MetaManagement.insertOrUpdateMetaAsync(updateModel).then(model=>{
this.model=this.fillMissing(model);
if(this.model.isValid){
this.hash=JSON.stringify(this.model);
NotificationStore.showSuccessMessage(Strings.general.saveSuccessMessage);
}
否则{
NotificationStore.showErrorMessage(model.validationMessage);
}
}).catch(错误=>{
NotificationStore.showErrorMessage(错误);
});
}
this.canSaveData=this.canSaveModel();
}
handleDelete=()=>{
DialogStore.show(Utilities.format(Strings.general.dialogDeleteTitle,this.model.caption.value),Utilities.format(Strings.general.dialogDeleteText,this.model
this.model.metaValues.push(newValue);