Skip to content

Instantly share code, notes, and snippets.

@svileng

svileng/app.jsx Secret

Created May 19, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svileng/1f3702df855b50ed2f5e to your computer and use it in GitHub Desktop.
Save svileng/1f3702df855b50ed2f5e to your computer and use it in GitHub Desktop.
Force Bacon.js stream update
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