juliangruber / level-list

Map lists of data stored in a LevelDB to DOM elements.
7 stars 1 forks source link

Problems mixing EventEmitter and data #3

Open morganrallen opened 10 years ago

morganrallen commented 10 years ago

By adding data properties straight to the EventEmitter there is no way to sanely serialize the object.

My Scenario

I'm creating an input based on a certain key, it will alter value and attempt to save the data. The problem that currently occurs is the json-serialization barfs (max call stack) trying to serialize the EventEmitter.

Code loosely looks like this.

List(db, function(row) {
  var el = document.createElement("div");
  var check = document.createElement("input");
  check.type = "checkbox";
  check.checked = row.active || false;
  check.addEventListener("change", function() {
    row.active = check.checked;
    db.put(row.id, row); // <---- cannot serialize
  });

  return el;
});

My suggestion would be keeping more closely aligned with level and store actually data on the EventEmitter under the .value key.

List(db, function(row) {
  row.on("update active", function() {
    console.log("active state changed to %s", row.value.active);
    // .... blah blah, do stuff
    db.put(row._key, row.value);
  });
});
juliangruber commented 10 years ago

Yeah it's kinda doomed to fail, also e.g. if you have a property called on in your row value which would overwrite the event emitter function. On the other hand, component/reactive bindings would become awkward then.

Would you mind making a pull request that still works with reactive?

morganrallen commented 10 years ago

Sorry, wont have time to address that, I'm not using or plan to use reactive.

On Tue, Nov 26, 2013 at 10:50 AM, Julian Gruber notifications@github.comwrote:

Yeah it's kinda doomed to fail, also e.g. if you have a property called onin your row value which would overwrite the event emitter function. On the other hand, component/reactive bindings would become awkward then.

Would you mind making a pull request that still works with reactive?

— Reply to this email directly or view it on GitHubhttps://github.com/juliangruber/level-list/issues/3#issuecomment-29320043 .

http://ithoughtyouweretherobot.com Metal and Wire

http://nolonelyguineapigs.com/ Wandering and Rambling

http://morglog.org Old and Neglected.

-----BEGIN PGP PUBLIC KEY BLOCK----- Version: OpenPGP.js v.1.20121007 Comment: http://openpgpjs.org

xsBNBFG3btUBB/9+/WJNOSIuc/praKPaPOweqXV5s7PGRD+HAnNWF/19YAY3 AFtfeoelhhY4sMJoobTaJzcZojznZ1kl/7UuuYCnbJO3Z9kaSMVrVxEZMSLe YmW8Hc4ZsTnl9f05DQFy8ABpAvMLmXJQXh63BbfzjFqNbSWQfLhrubSM2Elq xmN6EsmRyAuEeYlSnal+Di2MlViohpCbbagE2D6AZTUECIamTib6+DuLG4XO b4GGKHR9TXu2qS1VH6hqcvdEz2MDE5OqZzAnBgcF2dvcOdTk9gAOoR1T+qKj IJtg2X0ETbxcPqEXQoXE8VrxN0MLT5JQKTg26+csU4P2w0rEWsUsJombABEB AAHNJU1vcmdhbiBBbGxlbiA8bW9yZ2FucmFsbGVuQGdtYWlsLmNvbT7CwFwE EAECABAFAlG3btwJEG9pFLKaVEfeAAD7hwf+K8LN/kfgi8GTRLiTcuM+hSbm uAeiBY+VaJIQFLzoFlt8A0hBlLNPFNNmUDxdPrMErdHgZ7HtJW/6RANuBTti 5hr3EBguH1GQyK7BbUqJGvaEX4UNRej3uyX3ufXDsIB/3NsKaTyyu+SO0+Hv Cn2DwmlfSXcPhoarx6Turizn/WwgtTN3tqrMWxxiCKWydLH5xYuz+23BLMdc oF/zkbF+7ddFhmyfiFh4Ej9//tsfwPj2cACU3uLTrcwCo7IxUtkrqMF/fhlm LEqjhzDDjq9v7GNWb/5xB2xgTaucJ7/ljuS8nPlZqwCI8NlTgSqC8nyVLyjS yVtOzkItcS0UqyW1Mg== =V/Ia -----END PGP PUBLIC KEY BLOCK-----

morganrallen commented 10 years ago

I've looked at reactive briefly and it does not appear to rely on a stream being passed but just an object. Also I looked at the chat example, why would the following not work? I'm not seeing how moving the data to its own property would affect reactive as you have to explicitly has an object in anyhow.

return reactive(domify(tmpl), row.value).el
juliangruber commented 10 years ago

yeah true, I was thinking that the template could rely on row.key to compute urls, but if it does you should just also have your key tin the value.