fix key checker

This commit is contained in:
Mark Nadal 2016-01-25 20:22:32 -08:00
parent 3edfbebd39
commit a85a7a6999
5 changed files with 112 additions and 52 deletions

View File

@ -31,7 +31,6 @@
var gun = Gun(location.origin + '/gun');
var chat = gun.get('example/chat/data').not(function(key){
console.log("NOT KEY", key);
gun.put({1: {who: 'Welcome', what: "to the chat app!", when: 1}}).key(key);
});
chat.map().val(function(msg, field){

View File

@ -14,39 +14,38 @@
<span contenteditable="true">val</span>
</li>
<ul><li>
<form id="form" onsubmit="add()">
<form id="form">
<label>
<input id="field" placeholder="field">
<button type="submit">add</button>
</label>
</form>
</li></ul>
<script> // minimal jQuery polyfill
var $ = function(s, e){ return (e || document).querySelector(s) }
function clean(text){ return String(text).replace(/\</ig, '&lt;') }
</script>
<script>
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
var ref = Gun(location.origin + '/gun').get('example/json/data');
ref/*.not(function(key){
console.log("does NOT exist", key);
return this.put({hello: "world!"}).key(key);
})*/.on(function(data){
console.log("ugggh", data);
for(var field in data){
if(field === '_'){ continue } // skip meta data!
var val = (data[field = field || ''] || '').toString(), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe.
(elem = $('#' + id) || $('#data').appendChild($('#model').cloneNode(true))).id = id; // reuse or make element, set id.
$('b', elem).innerHTML = field.replace(/\</ig, '&lt;'); // escape and display field
$('span', elem).innerHTML = val.replace(/\</ig, '&lt;'); // escape and display value
}
});
var gun = Gun(location.origin + '/gun');
var ref = gun.get('example/json/data');
$('#form').onsubmit = function(e){
return ref.path( clean($('#field').value) ).put("value"), false; // add a new field, and cancel the form submit.
}
document.onkeyup = function(e){
if(!e || !e.target){ return } // ignore if no element!
if(!e.target.attributes.contenteditable){ return } // ignore if element content isn't editable!
ref.path((e.target.previousElementSibling.innerHTML||'').toString().replace(/\</ig, '&lt;')) // grab the label, which is in the previous element.
.put( (e.target.innerHTML||'').toString().replace(/\</ig, '&lt;') ); // insert the value of the text in the current element.
}
$('#form').onsubmit = function add(e){
return ref.path($('#field').value || '').put("value"), false; // add a new field, and cancel the form submit.
ref.path(clean(e.target.previousElementSibling.innerHTML)) // grab the label, which is in the previous element.
.put( clean(e.target.innerHTML) ); // insert the value of the text in the current element.
}
ref.on(function(data){
delete data._; // skip meta data!
for(var field in data){
var val = String(data[field]), id = field.replace(/[^A-z]/ig, ''), elem; // make data safe.
(elem = $('#' + id) || $('#data').appendChild($('#model').cloneNode(true))).id = id; // reuse or make element, set id.
$('b', elem).innerHTML = clean(field); // escape and display field
$('span', elem).innerHTML = clean(val); // escape and display value
}
});
</script>
<style>
html, body {

View File

@ -1,38 +1,37 @@
<html>
<body onload="ready()">
<body>
<h2>ToDo List</h2>
<form id="addToDo"><input id="todoItem" /><button>Add</button></form>
<form id="add"><input id="todo"/><button>Add</button></form>
<ul id="todos"></ul>
<script src="../../gun.js"></script>
<script>
// by Forrest Tait! Edited by Mark Nadal.
function ready(){
<script> // minimal jQuery polyfill
var $ = document.querySelector.bind(document);
var gun = Gun(location.origin + '/gun').get('example/todo/data');
gun.on(function renderToDo(val){
var todoHTML = '';
for(field in val) {
if(!val[field] || field == '_') continue;
todoHTML += '<li style="width:400px;height:2em;">'
+ (val[field]||'').toString().replace(/\</ig, '&lt;')
+ '<button style="float:right;" onclick=removeToDo("'+field+'")>X</button></li>';
}
$("#todos").innerHTML = todoHTML;
});
$("#addToDo").onsubmit = function(){
gun.path(Gun.text.random()).put(($("#todoItem").value||'').toString().replace(/\</ig, '&lt;'));
$("#todoItem").value = "";
return false;
function clean(text){ return String(text).replace(/\</ig, '&lt;') }
</script>
<script> // by Forrest Tait! Edited by Mark Nadal.
var gun = Gun(location.origin + '/gun');
var todo = gun.get('example/todo/data');
$("#add").onsubmit = function(){
todo.path(Gun.text.random()).put(clean($("#todo").value)); // add the HTML input's value to a random ID in the todo.
$("#todo").value = ""; // clear out the input's value so we can add more.
return false; // prevent the browser from reloading.
};
window.removeToDo = function(id){
gun.path(id).put(null);
todo.on(function(list){ // subscribe and listen to all updates on the todo.
var html = ''; // old school HTML strings! You should probably use a real template system.
for(field in list) { // iterate over the list to generate the HTML.
if(!list[field] || field == Gun._.meta) continue; // ignore nulled out values and metadata.
html += '<li>'
+ clean(list[field])
+ '<button style="float:right;" onclick=todone("'+field+'")>X</button>'
+ '</li>';
}
$("#todos").innerHTML = html; // set the HTML to our new list.
});
window.todone = function(field){
todo.path(field).put(null); // null out the todo item on this field ID.
}
}
function randomId(){
return ''+(new Date()).getTime()+Math.round((Math.random()*1000));
}
</script>
</body>
</html>

5
gun.js
View File

@ -656,7 +656,8 @@
ctx.by = gun.__.by(ctx.soul);
ctx.by.chain = ctx.by.chain || gun.chain();
function load(lex){
var cached = gun.__.by(lex[Gun._.soul]).node;
var soul = lex[Gun._.soul];
var cached = gun.__.by(soul).node || gun.__.graph[soul];
if(ctx.force){ ctx.force = false }
else if(cached){ return false }
wire(lex, stream, ctx.opt);
@ -719,8 +720,8 @@
at.change = Gun.is.node.soul.ify(Gun.obj.copy(at.change || gun.__.by(at.soul).node), at.soul, true); // TODO: Check performance?
return;
}
if(!(Gun.is.node.soul(gun.__.graph[at.soul], 'key') === 1)){ return }
var node = at.change || gun.__.graph[at.soul];
if(!(Gun.is.node.soul(node, 'key') === 1)){ return }
function map(rel, soul){ gun.__.gun.get(rel, cb, {key: ctx, chain: opt.chain || gun, force: opt.force}) }
ctx.halt = true;
Gun.is.node(node, map);

View File

@ -3525,7 +3525,69 @@ describe('Gun', function(){
chat.path(msg.when + '_' + Gun.text.random(4)).put(msg);
},100);
});
/* // This test didn't work for what I was wanting to test :(, will either remove it or modify it if I think of a clever solution to test what I want to test.
it.only("simulate json app", function(done){
var peers = {};
peers.server = Gun();
function wipeServer(){
peers.server = Gun();
}
var gopt = {wire:{
put: function(graph, cb){
Gun.union(peers.server, graph);
cb(null);
}
,get: function(lex, cb){
setTimeout(function(){
var soul = lex[Gun._.soul];
if(peers.localStorage){
var g = peers.localStorage;
console.log("VIA LOCALSTORAGE!", lex, g[soul]);
if(g[soul]){
var n = g[soul];
cb(null, n);
cb(null, Gun.is.node.ify({}, soul));
cb(null, {});
}
}
setTimeout(function(){
var graph = peers.server.__.graph;
console.log("VIA the SERVER!!", lex, graph[soul]);
if(!graph[soul]){
cb(null);
cb(null, {});
return;
}
var node = graph[soul];
cb(null, node);
cb(null, Gun.is.node.ify({}, soul));
cb(null, {});
},5);
},5);
}
}}
peers.gun = Gun(gopt);
function reload(){
peers.localStorage = Gun.obj.copy(peers.gun.__.graph);
peers.gun2 = Gun(gopt);
}
var ref = peers.gun.get('example/json/data/test');
setTimeout(function(){
ref.path('hello').put("value");
setTimeout(function(){
wipeServer();
reload();
setTimeout(function(){
Gun.log.debug = 1; console.log("~~~~~~~~~~~~~~~~~~~");
var ref = peers.gun2.get('example/json/data/test');
ref.on(function(data){
console.log("on!", data);
});
},100);
},100);
},100);
});
*/
it("simulate chat app", function(done){
var server = Gun();
var gopt = {wire:{