-
-
Save svileng/1f3702df855b50ed2f5e to your computer and use it in GitHub Desktop.
Force Bacon.js stream update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require("react") | |
var Comments = require("./comments/comments.jsx") | |
var Bacon = require("baconjs") | |
React.render(<Comments comments={[]}/>, document.getElementById("app")) | |
document.addEventListener("DOMContentLoaded", (event) => { | |
var buttonClickS = Bacon.fromEvent(document.getElementById("add-comment"), "click") | |
var newCommentValueS = Bacon.fromEvent(document.getElementById("new-comment"), "input").map(e => e.target.value) | |
var newCommentPresent = newCommentValueS.map(value => value !== "").toProperty(false) | |
var newComment = newCommentValueS.filter(v => v.length > 0).toProperty("") | |
var commentAdded = Bacon.when( | |
[newComment, buttonClickS], (comment, click) => { | |
console.log("Submit comment: " + comment) | |
document.getElementById("new-comment").value = "" | |
return comment | |
} | |
) | |
// Enables/disables the submit button. | |
newCommentPresent.onValue(function(enabled) { | |
var el = document.getElementById("add-comment") | |
if (enabled) { | |
el.removeAttribute("disabled") | |
} else { | |
el.setAttribute("disabled", "disabled") | |
} | |
}) | |
// Comments submitted. | |
var comments = commentAdded.scan([], (result, comment) => { | |
result.push(comment) | |
return result | |
}) | |
var reRender = Bacon.combineTemplate({ | |
comments: comments | |
}) | |
reRender.onValue((state) => { | |
React.render(<Comments {...state}/>, document.getElementById("app")) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment