Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement stateful property bindings

  • Loading branch information...
commit fb26051574f8e6fba75cf1af31f252c204150b7c 1 parent a018791
@kriszyp authored
Showing with 80 additions and 1 deletion.
  1. +32 −1 bind.js
  2. +21 −0 tests/testStateful.html
  3. +27 −0 tests/testStateful.js
View
33 bind.js
@@ -16,6 +16,7 @@ define([], function(){
callback(this.value);
},
get: function(key, callback){
+ // use an existing child/property if it exists
var child = this['_' + key] || (this.source ?
this.source.get(key) :
(this['_' + key] = this.value && typeof this.value == "object" ?
@@ -79,7 +80,7 @@ define([], function(){
});
return this;
}
- };
+ };
// StatefulBinding is used for binding to Stateful objects, particularly Dijit widgets
function StatefulBinding(stateful){
this.stateful = stateful;
@@ -99,6 +100,36 @@ define([], function(){
}
});
return this;
+ };
+ StatefulBinding.prototype.get = function(key, callback){
+ return this['_' + key] || (this['_' + key] = new StatefulPropertyBinding(this.stateful, key));
+ };
+
+ function StatefulPropertyBinding(stateful, name){
+ this.stateful = stateful;
+ this.name = name;
+ }
+ StatefulPropertyBinding.prototype = new Binding;
+ StatefulPropertyBinding.prototype.getValue = function(callback){
+ // get the value of this property
+ var stateful = this.stateful,
+ name = this.name,
+ binding = this;
+ // get the current value
+ callback(stateful.get(name));
+ // watch for changes
+ stateful.watch(name, function(name, oldValue, newValue){
+ Binding.prototype.is.call(binding, newValue);
+ });
+ };
+ StatefulPropertyBinding.prototype.is = function(value){
+ // don't go through setters, it is bubbling up through the source
+ this.stateful._changeAttrValue(this.name, value);
+ }
+ StatefulPropertyBinding.prototype.put = function(value){
+ // put a value, go through setter
+ this.stateful.set(this.name, value);
+ }
function ElementBinding(element, container){
this.element= element;
View
21 tests/testStateful.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Test dbind with a Stateful object and a computed property</title>
+ <style type="text/css">
+ @import "../../dojo/resources/dojo.css";
+ .error-message {
+ color: red;
+ }
+ </style>
+ <script type="text/javascript" src="../../dojo/dojo.js"
+ data-dojo-config="async: true"></script>
+ <script type="text/javascript">
+ require(["./testStateful.js", "dojo/dom", "dojo/domReady!"], function(testForm, dom){
+ });
+ </script>
+ </head>
+ <body>
+ Check the console for output
+ </body>
+</html>
View
27 tests/testStateful.js
@@ -0,0 +1,27 @@
+define(['dbind/bind', 'dojo/Stateful'], function(bind, Stateful){
+ function Model(props) {
+ var stateful = new Stateful(props),
+ first = bind(stateful, 'first'),
+ last = bind(stateful, 'last'),
+ fullName = bind(stateful, 'fullName').to(bind(function (first, last) {
+ console.log('making full name', first, last);
+ return [].join.apply(arguments);
+ }).to([first, last]));
+
+ fullName.then(function(fullName){
+ console.log("The full name is now", fullName);
+ });
+ return stateful;
+ }
+
+ model = Model({
+ first: 'first',
+ last: 'last'
+ });
+ console.log(model);
+
+ model.set("first", "John");
+ model.set("last", "Doe");
+
+ console.log("Full name: ", model.fullName);
+});
Please sign in to comment.
Something went wrong with that request. Please try again.