Last active
December 31, 2015 16:19
-
-
Save k2052/8012927 to your computer and use it in GitHub Desktop.
Revisions
-
k2052 revised this gist
Dec 17, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ class DataBinder extends jQuery constructor: (objectid) -> @pubSub = $ {} attribute = "data-bind-#{objectid}" message = "#{objectid}:change" $(document).on "change", "[#{attribute}]", (event) => @@ -42,4 +42,4 @@ class Cat cat = new Cat("cat-1") cat.set("name", "Wiswell") # HTML <input type="text" data-bind-cat-1="name" /> -
k2052 created this gist
Dec 17, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ class DataBinder extends jQuery pubSub: null constructor: (objectid) -> @pubSub = $ {} attribute = "bind-#{objectid}" message = "#{objectid}:change" $(document).on "change", "[#{attribute}]", (event) => $elem = $(event.target) @pubSub.trigger message, [$elem.attr(attribute), $elem.val()] @pubSub.on message, (event, property, new_val) -> for elem in $("[#{attribute}=#{property}]") $elem = $(elem) if $elem.is("input, textarea, select") $elem.val new_val else $elm.html new_val class Cat binder: null pubSub: null message: '' set: (attr, val) -> @[attr] = val @pubSub.trigger @message, [attr, val] get: (attr) -> @[attr] constructor: (id) -> @binder = new DataBinder(id) @pubSub = @binder.pubSub @message = "#{id}:change" @pubSub.on @message, (event, attr, value) => @[attr] = value cat = new Cat("cat-1") cat.set("name", "Wiswell") # HTML <input type="text" bind-cat-1="name" />