Skip to content

Instantly share code, notes, and snippets.

@zalavariandris
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zalavariandris/9067740 to your computer and use it in GitHub Desktop.
Save zalavariandris/9067740 to your computer and use it in GitHub Desktop.
two way binding with contenteditable in Meteor (concept)

Bindings With Meteor

Two way bindings between Collections and contenteditable elements With Meteor UI

I've been missing two way bindings from Meteor. I wanted to figure out the lightest way to achive two way bindings with a contenteditable Span. This seems to be working, and easy super easy to integrate.

Misses even basic Error handling and a little more attention updating the innerText of the span element

Usage

{{>Edit name="propertyName"}}

if Meteor.isClient
Template.EditText = UI.Component.extend
kind: "Edit"
init: ()->
self = this
self.textDep = new Deps.Dependency
self.getText = ()->
self.textDep.depend()
self.text
Deps.autorun ()->
data = self.get()
propertyName = self.get("bind")
value = data.get(propertyName)
unless self.isFocused
self.text = value
self.textDep.changed()
self.events =
keydown: (event, template)->
if event.keyCode == 13
event.preventDefault()
event.stopImmediatePropagation()
input: (event, template)->
model = self.get()
propertyName = self.get("bind")
json = {}
json[propertyName] = $(event.target).text()
model.update {$set: json}
focus: ()->
self.isFocused = true
blur: ()->
self.isFocused = false
render: ()->
# UI.materialize accepts
# ----------------------
# - string, boolean, number
# - Array
# - function, (with reactivity)
# - Components
# - HTML.Tag, HTML.Raw, HTML.Comment, HTML.CharRef
self = this
return HTML.DIV {
contenteditable: ()-> !self.get("disabled")
},
()-> return self.getText()
<template name="Edit">
<span contenteditable name='{{name}}' placeholder='{{name}}' class="Edit"></span>
</template>
# DB Collection
collection = new Meteor.Collection "collection",
transform: (doc)->
return new Model doc
# Model class
class Model
constructor: (doc)->
for key in Object.keys doc
this[key] = doc[key]
update: (modifier, options, callback)->
collection.update this._id, modifier, options, callback
@dbackeus
Copy link

This gist is broken in latest Meteor due to changes in the Blaze api etc. Any chance of a rewrite to support it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment