用例?
Firebase
有一个新产品Firestore
,根据其文档启用offlinePersistence
。
我想测试应用程序加载的情况,但是没有与firebase建立连接(想想服务工作者使用缓存的静态资产的Progressive Web App),但没有连接到Firebase的网络。
我的代码看起来像
import React from "react";
import {fdb} from "../mainPage/constants";
// includeDocumentMetadataChanges to know when backend has written the local changes
// includeQueryMetadataChanges to know if changes come from cache using 'fromCache' property
// https://firebase.google.com/docs/firestore/manage-data/enable-offline?authuser=0#listen_to_offline_data
const queryOptions = {
includeDocumentMetadataChanges: true,
includeQueryMetadataChanges: true
};
const collectionName = "todos";
export default class ToDos extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
textBox: "",
loading: true
}
}
componentWillMount() {
let unsubscribe = fdb.collection(collectionName)
.onSnapshot(queryOptions, function (querySnapshot) {
let items = [];
querySnapshot.forEach(function (doc) {
items.push(doc);
//console.log(" data: ", doc && doc.data());
});
this.setState({"items": items});
}.bind(this));
this.setState({"unsubscribe": unsubscribe});
}
componentWillUnmount() {
this.state.unsubscribe();
}
handleTextBoxChange = (event) => {
this.setState({textBox: event.target.value});
};
handleAddItem = () => {
fdb.collection(collectionName).add({
"title": this.state.textBox
}).then(function (docRef) {
//console.log("added " + docRef.id, docRef.get());
});
};
handleRemoveItem = (item) => {
console.log("Deleting: ", item.id);
item.ref.delete()
.then(function () {
console.log(item.id + " deleted successfully");
})
.catch(function(reason){
console.log(item.id + " failed to delete: ", reason);
})
};
render() {
return (
<div>
<div>
<input type="text" value={this.state.textBox} onChange={this.handleTextBoxChange}/>
<input type="submit" value="Add Item" onClick={this.handleAddItem}/>
</div>
<div>{this.state.items
.map((item, index) => <Item key={index}
item={item}
onDeleteClick={this.handleRemoveItem}/>)}</div>
</div>
)
}
}
const Item = ({item, onDeleteClick}) => {
let pendingWrite = item.metadata.hasPendingWrites ? "[PendingWrite]" : "[!PendingWrite]";
let source = item.metadata.fromCache ? "[Cache]" : "[Server]";
return <div>
<input type="button" value="delete" onClick={() => onDeleteClick(item)}/>
<span>{source + " " + pendingWrite + " " + item.data().title}</span>
</div>
};
问题
Chrome开发者工具中是否有办法模拟/禁用对特定域的调用?