Commits

Anonymous committed 4e3b1a6

experimental datalink work.

Comments (0)

Files changed (3)

             
             subviews_promise.done(function() {
                 self.bind_events();
+                self.datalinks();
                 
                 self.finalize();
                 // the callback needs to be the last thing called
             el.unbind(event_info.name, event_info.handler);
         });
     };
+
+    module.View.prototype.datalinks = function() {
+        var self = this;
+        $.each(self.links, function(selector, datalink_info) {
+            self.datalink(selector, datalink_info);
+        });
+    };
+
+    var auto_id = 0;
+    
+    module.View.prototype.datalink = function(selector, datalink_info) {
+        var self = this;
+        var els = $(selector, self.el);
+
+        if (els.length == 0) {
+            return;
+        }
         
+        var link_context = {};
+
+        // XXX goes wrong with multiple els?
+        var linked_obj = $(self.obj);
+        els.each(function(index, el) {
+            el = $(el);
+            var id = el.attr('id');
+            if (id === undefined) {
+                id = 'auto-id-' + auto_id.toString();
+                el.attr('id', id);
+                auto_id++;
+            }
+            link_context[datalink_info] = {
+                twoWay: true,
+                name: id,
+                convertBack: function(value, source, target) {
+                    $(target).text(value);
+                }
+            };
+            el.link(self.obj, link_context);
+            var existing_value = self.obj[datalink_info];
+            if (existing_value !== undefined) {
+                linked_obj.setField(datalink_info, existing_value);
+            }
+        });
+        self.liveobj = linked_obj;
+    };
+    
     module.View.prototype.finalize = function() {
         var self = this;
         self.store_view();

test/manual/datalink.html

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />
+    <title>Manual checking datalink in obviel.js</title>
+    <script type="text/javascript" src="../dependencies/jquery-1.6.1.js"></script>
+    <script type="text/javascript" src="../dependencies/jquery.datalink.js"></script>
+    <script type="text/javascript" src="../../src/json-template.js"></script>
+    <script type="text/javascript" src="../../src/obviel.js"></script>
+    <script type="text/javascript" src="datalink.js"></script>
+  </head>
+  <body>
+    <div id="test"></div>
+  </body>
+</html>

test/manual/datalink.js

+
+$(document).ready(function() {
+
+    obviel.view({
+        html: '<div id="alpha"></div><div id="beta"></div>',
+        links: {
+            '#alpha': 'alpha',
+            '#beta': 'beta'
+        },
+        events: {
+            '#alpha': {
+                name: 'click',
+                handler: function(ev) {
+                    ev.view.liveobj.setField('beta', 'Changed');
+                }
+            }
+        }
+    });
+    
+    $('#test').render({'alpha': 'ALPHA', 'beta': 'BETA'});
+
+});