mirror of https://github.com/docker/docs.git
Fixing search with promises
This commit is contained in:
parent
da27dd07a2
commit
f3673a5588
|
@ -8,56 +8,57 @@ var assign = require('object-assign');
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
|
|
||||||
var _recommended = [];
|
var _recommended = [];
|
||||||
|
var _searchPromise = null;
|
||||||
|
|
||||||
var NewContainer = React.createClass({
|
var NewContainer = React.createClass({
|
||||||
_searchRequest: null,
|
|
||||||
getInitialState: function () {
|
getInitialState: function () {
|
||||||
return {
|
return {
|
||||||
query: '',
|
query: '',
|
||||||
results: _recommended,
|
results: [],
|
||||||
loading: false,
|
loading: false,
|
||||||
tags: {},
|
tags: {}
|
||||||
active: null
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
componentDidMount: function () {
|
componentDidMount: function () {
|
||||||
this.refs.searchInput.getDOMNode().focus();
|
this.refs.searchInput.getDOMNode().focus();
|
||||||
if (!_recommended.length) {
|
|
||||||
this.recommended();
|
this.recommended();
|
||||||
|
},
|
||||||
|
componentWillUnmount: function () {
|
||||||
|
if (_searchPromise) {
|
||||||
|
_searchPromise.cancel();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
search: function (query) {
|
search: function (query) {
|
||||||
if (this._searchRequest) {
|
if (_searchPromise) {
|
||||||
this._searchRequest.abort();
|
_searchPromise.cancel();
|
||||||
this._searchRequest = null;
|
_searchPromise = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!query.length) {
|
if (!query.length) {
|
||||||
|
this.setState({
|
||||||
|
query: query,
|
||||||
|
results: _recommended,
|
||||||
|
loading: false
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
|
query: query,
|
||||||
loading: true
|
loading: true
|
||||||
});
|
});
|
||||||
|
|
||||||
var self = this;
|
_searchPromise = Promise.delay(200).then(() => Promise.resolve($.get('https://registry.hub.docker.com/v1/search?q=' + query))).cancellable().then(data => {
|
||||||
this._searchRequest = $.get('https://registry.hub.docker.com/v1/search?q=' + query, function (result) {
|
this.setState({
|
||||||
self.setState({
|
results: data.results,
|
||||||
query: query,
|
query: query,
|
||||||
loading: false
|
loading: false
|
||||||
});
|
});
|
||||||
self._searchRequest = null;
|
_searchPromise = null;
|
||||||
if (self.isMounted()) {
|
}).catch(Promise.CancellationError, () => {
|
||||||
self.setState(result);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
recommended: function () {
|
recommended: function () {
|
||||||
if (this._searchRequest) {
|
|
||||||
this._searchRequest.abort();
|
|
||||||
this._searchRequest = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_recommended.length) {
|
if (_recommended.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -88,23 +89,10 @@ var NewContainer = React.createClass({
|
||||||
},
|
},
|
||||||
handleChange: function (e) {
|
handleChange: function (e) {
|
||||||
var query = e.target.value;
|
var query = e.target.value;
|
||||||
|
|
||||||
if (query === this.state.query) {
|
if (query === this.state.query) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.search(query);
|
||||||
clearTimeout(this.timeout);
|
|
||||||
if (!query.length) {
|
|
||||||
this.setState({
|
|
||||||
query: query,
|
|
||||||
results: _recommended
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
var self = this;
|
|
||||||
this.timeout = setTimeout(function () {
|
|
||||||
self.search(query);
|
|
||||||
}, 200);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
handleClick: function (name) {
|
handleClick: function (name) {
|
||||||
ContainerStore.create(name, 'latest', function (err) {
|
ContainerStore.create(name, 'latest', function (err) {
|
||||||
|
@ -115,9 +103,6 @@ var NewContainer = React.createClass({
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
handleDropdownClick: function (name) {
|
handleDropdownClick: function (name) {
|
||||||
this.setState({
|
|
||||||
active: name
|
|
||||||
});
|
|
||||||
if (this.state.tags[name]) {
|
if (this.state.tags[name]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,7 +165,6 @@ var SetupStore = assign(Object.create(EventEmitter.prototype), {
|
||||||
run: Promise.coroutine(function* () {
|
run: Promise.coroutine(function* () {
|
||||||
yield this.updateBinaries();
|
yield this.updateBinaries();
|
||||||
var steps = yield this.requiredSteps();
|
var steps = yield this.requiredSteps();
|
||||||
console.log(steps);
|
|
||||||
for (let step of steps) {
|
for (let step of steps) {
|
||||||
console.log(step.name);
|
console.log(step.name);
|
||||||
_currentStep = step;
|
_currentStep = step;
|
||||||
|
|
Loading…
Reference in New Issue