收听更新
var node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4')
node.get('stats').on(function (v, k) {
console.log('v:', v);
console.log('k:', k);
});
更新
node.put({
stats: {
num: 3
},
name: 'trex'
});
如果您node
多次使用同一个对象更新 ,则每次在侦听器端都有更新。
{"_":{"#":"j94c5jht6u6KTIoltGt7mHx",">":{"num":1508935385873.435}},"num":3}
{"_":{"#":"j94c5jht6u6KTIoltGt7mHx",">":{"num":1508935385873.435}},"num":3}
{"_":{"#":"j94c5jht6u6KTIoltGt7mHx",">":{"num":1508935386550.19}},"num":3}
仅当属性值更改时如何接收更新?
var gun = new Gun();
var result = document.getElementById('result');
var node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4');
node.get('stats').on(function (v, k) {
displayValue(v);
console.log('v:', v);
console.log('k:', k);
});
function displayValue (v) {
var p = document.createElement("p");
var t = document.createTextNode(JSON.stringify(v));
p.appendChild(t);
result.appendChild(p);
};
function createNode () {
node.put({
stats: {
num: 3
},
name: 'trex'
});
}
function updateNode() {
node.put({
stats: {
num: 3
},
name: 'trex'
});
}
function deleteNode() {
while (result.firstChild) {
result.removeChild(result.firstChild);
}
}
var createNodeBtn = document.getElementById('create_node');
createNodeBtn.onclick = createNode;
var updateNodeBtn = document.getElementById('update_node');
updateNodeBtn.onclick = updateNode;
var deleteNodeBtn = document.getElementById('delete_node');
deleteNodeBtn.onclick = deleteNode;
<script src="https://rawgit.com/amark/gun/master/gun.js"></script>
<button type="button" id="create_node">create</button>
<button type="button" id="update_node">update</button>
<button type="button" id="delete_node">clean</button>
<div id=result></div>