Commit 02cdb053 by hfpp2012 yinsigan

computed get method

1 parent 86007275
...@@ -8,19 +8,19 @@ import { observer, inject } from 'mobx-react'; ...@@ -8,19 +8,19 @@ import { observer, inject } from 'mobx-react';
@autobind @autobind
export default class ElementGroup extends React.Component { export default class ElementGroup extends React.Component {
cloneElement(element) { cloneElement(element) {
this.store().cloneElement(element) this.store.cloneElement(element)
} }
deleteElement(element) { deleteElement(element) {
this.store().deleteElement(element) this.store.deleteElement(element)
} }
// 返回element的index为-1, 删除最后一个元素 // 返回element的index为-1, 删除最后一个元素
deleteLastElement() { deleteLastElement() {
this.store().deleteLastElement() this.store.deleteLastElement()
} }
store() { get store() {
return this.props.elementGroupStore return this.props.elementGroupStore
} }
...@@ -31,7 +31,7 @@ export default class ElementGroup extends React.Component { ...@@ -31,7 +31,7 @@ export default class ElementGroup extends React.Component {
render() { render() {
return ( return (
<div> <div>
{this.store().elements.map(element => {this.store.elements.map(element =>
(<Element (<Element
meta_element={ element } meta_element={ element }
key={ "key_" + element.id } key={ "key_" + element.id }
......
import React from 'react' import React from 'react'
import ModalRepoForm from './ModalRepoForm' import ModalRepoForm from './ModalRepoForm'
import Loader from 'halogen/PulseLoader'
import { Link } from 'react-router-dom'
import { observer, inject } from 'mobx-react'; import { observer, inject } from 'mobx-react';
import autobind from 'autobind-decorator' import autobind from 'autobind-decorator'
@autobind @autobind
@inject(["headerStore"]) @observer @inject(["headerStore"]) @observer
export default class Header extends React.Component { export default class Header extends React.Component {
get store() {
return this.props.headerStore
}
componentDidMount() { componentDidMount() {
this.props.headerStore.loadHeaderData() this.store.loadHeaderData()
} }
addRepo(repo) { addRepo(repo) {
this.props.headerStore.addRepo(repo) this.store.addRepo(repo)
} }
render() { render() {
const { headerStore } = this.props;
let result = null;
if (headerStore.loading) {
result = <Loader color="#286090" size="12px" margin="4px"/>;
}
else if (headerStore.error !== null) {
result = <span>Error: {headerStore.error}</span>;
}
else {
const data = headerStore.data.map((repo) => {
const url = `/web/regional_design/${repo.id}?object=repo`
return (
<Link key={ repo.id } className='btn btn-default mr15' to={ url }>{ repo.name }</Link>
)
})
result = data;
}
return ( return (
<div className="text-left top-header"> <div className="text-left top-header">
<ModalRepoForm handleNewRepo={ this.addRepo } /> <ModalRepoForm handleNewRepo={ this.addRepo } />
<div className='col-md-10'> <div className='col-md-10'>
{ result } { this.store.showElements }
</div> </div>
<div className="clearfix"></div> <div className="clearfix"></div>
</div> </div>
......
...@@ -8,24 +8,24 @@ import { observer, inject } from 'mobx-react'; ...@@ -8,24 +8,24 @@ import { observer, inject } from 'mobx-react';
@observer @observer
@autobind @autobind
export default class ModalRepoForm extends React.Component { export default class ModalRepoForm extends React.Component {
store() { get store() {
return this.props.modalRepoFormStore return this.props.modalRepoFormStore
} }
close() { close() {
this.store().close(); this.store.close();
} }
open() { open() {
this.store().open(); this.store.open();
} }
handleChange(e) { handleChange(e) {
this.store().handleChange(e); this.store.handleChange(e);
} }
save(e) { save(e) {
this.store().save(e, this.refs.name, this.props.headerStore) this.store.save(e, this.refs.name, this.props.headerStore)
} }
render() { render() {
...@@ -39,7 +39,7 @@ export default class ModalRepoForm extends React.Component { ...@@ -39,7 +39,7 @@ export default class ModalRepoForm extends React.Component {
新建仓库 新建仓库
</Button> </Button>
<Modal backdrop='static' show={this.store().showModal} onHide={this.close}> <Modal backdrop='static' show={this.store.showModal} onHide={this.close}>
<Modal.Header closeButton> <Modal.Header closeButton>
<Modal.Title>新建仓库</Modal.Title> <Modal.Title>新建仓库</Modal.Title>
</Modal.Header> </Modal.Header>
...@@ -47,9 +47,9 @@ export default class ModalRepoForm extends React.Component { ...@@ -47,9 +47,9 @@ export default class ModalRepoForm extends React.Component {
<form className='form-horizontal'> <form className='form-horizontal'>
<label htmlFor="formHorizontalName" className="col-sm-2 control-label">仓库名</label> <label htmlFor="formHorizontalName" className="col-sm-2 control-label">仓库名</label>
<div className="col-sm-5"> <div className="col-sm-5">
<input autoFocus type="text" id="formHorizontalName" className="form-control" ref='name' value={ this.store().name } onChange={ this.handleChange } /> <input autoFocus type="text" id="formHorizontalName" className="form-control" ref='name' value={ this.store.name } onChange={ this.handleChange } />
{function(){ {function(){
if (this.store().showNameError) { if (this.store.showNameError) {
return <p className="text-danger">仓库名不能为空</p> return <p className="text-danger">仓库名不能为空</p>
} }
}.call(this)} }.call(this)}
......
import { observable, useStrict, action, runInAction } from 'mobx' import { observable, useStrict, action, runInAction, computed } from 'mobx'
import React from 'react'
import Loader from 'halogen/PulseLoader'
import { Link } from 'react-router-dom'
import $ from 'jquery' import $ from 'jquery'
useStrict(true) useStrict(true)
...@@ -27,6 +30,29 @@ class HeaderStore { ...@@ -27,6 +30,29 @@ class HeaderStore {
}) })
}) })
} }
@computed get showElements() {
let result = null;
if (this.loading) {
result = <Loader color="#286090" size="12px" margin="4px"/>;
}
else if (this.error !== null) {
result = <span>Error: {this.error}</span>;
}
else {
const data = this.data.map((repo) => {
const url = `/web/regional_design/${repo.id}?object=repo`
return (
<Link key={ repo.id } className='btn btn-default mr15' to={ url }>{ repo.name }</Link>
)
})
result = data;
}
return result
}
} }
export default new HeaderStore() export default new HeaderStore()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!