Permalink
Browse files

working with RDF resources inside templates WIP

  • Loading branch information...
1 parent 0a3e322 commit 642d4d4a4d5c36849d622fa362c02cf63aa33448 @antoniogarrote committed Aug 8, 2011
@@ -1800,7 +1800,10 @@ ko.templateRewriting = (function () {
var tagToRetain = arguments[1];
var dataBindAttributeValue = arguments[6];
- dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
+ // @modified
+ // modified the rewritting function used
+ //dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
+ dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyReaderWritersIntoJson(dataBindAttributeValue);
// For no obvious reason, Opera fails to evaluate dataBindAttributeValue unless it's wrapped in an additional anonymous function,
// even though Opera's built-in debugger can evaluate it anyway. No other browser requires this extra indirection.
@@ -1813,8 +1816,14 @@ ko.templateRewriting = (function () {
applyMemoizedBindingsToNextSibling: function (bindings) {
return ko.memoization.memoize(function (domNode, viewModel) {
- if (domNode.nextSibling)
- ko.applyBindingsToNode(domNode.nextSibling, bindings, viewModel);
+ if (domNode.nextSibling) {
+ // @modified
+ sko.traceResources(domNode.nextSibling, viewModel, function(){
+ sko.traceRelations(domNode.nextSibling, viewModel, function(){
+ ko.applyBindingsToNode(domNode.nextSibling, bindings, viewModel);
+ });
+ });
+ }
});
}
}
@@ -1849,18 +1858,21 @@ ko.exportSymbol('ko.templateRewriting.applyMemoizedBindingsToNextSibling', ko.te
if ((typeof renderedNodesArray.length != "number") || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType != "number"))
throw "Template engine must return an array of DOM nodes";
- if (renderedNodesArray)
- ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
- ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode, [data]);
- });
-
+ // @modified
+ // Change the positoin of switch and if(render
+ // so the rendered node is added to the DOM before being unmemoized
switch (renderMode) {
case "replaceChildren": ko.utils.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray); break;
case "replaceNode": ko.utils.replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray); break;
case "ignoreTargetNode": break;
default: throw new Error("Unknown renderMode: " + renderMode);
}
+ if (renderedNodesArray)
+ ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
+ ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode, [data]);
+ });
+
if (options['afterRender'])
options['afterRender'](renderedNodesArray, data);

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -86,6 +86,9 @@ sko.about = function(aboutValue, viewModel, cb) {
// register the new observer and resource
sko.store.node(sko.plainUri(uri), function(success, resource) {
if(success) {
+ console.log("FOUND RESOURCE: "+uri);
+ console.log(resource);
+ console.log(" FOR "+sko.plainUri(uri));
// id -> Resource
sko.aboutResourceMap[nextId] = new sko.Resource(uri,resource);
// id -> observer
@@ -197,9 +200,9 @@ sko.rel = function(relValue, node, viewModel, cb) {
read: function(){
console.log("*** OBSERVABLE READING RELATED DEPENDING NODE ABOT ID:"+nextId);
var resource = sko.currentResource(jQuery(node).parent().toArray()[0]);
- console.log(" ** about:"+resource.about());
console.log(resource);
if(resource != null) {
+ console.log(" ** about:"+resource.about());
console.log("*** Found parent resource: "+resource.about());
if(resource[uri]) {
@@ -276,16 +279,21 @@ sko.rel = function(relValue, node, viewModel, cb) {
console.log(" ** NEXT URI IS NULL, GEN BLANK LABEL");
nextUri = sko.nextBlankLabel();
}
-
- if(sko.plainUri(nextUri) !== sko.plainUri(sko.about[nextId]())) {
- sko.store.node(sko.plainUri(nextUri), function(success, nextResource) {
- if(success) {
- var newUri = nextResource.toArray()[0].subject.valueOf();
- sko.aboutResourceMap[nextId].about(newUri);
- } else {
- console.log("Error updating 1 resource for URI:"+nextUri+" in SKO about related node observer");
- }
- });
+
+ if(sko.about[nextId]() != null) {
+ if(sko.plainUri(nextUri) !== sko.plainUri(sko.about[nextId]())) {
+ sko.store.node(sko.plainUri(nextUri), function(success, nextResource) {
+ if(success) {
+ var newUri = nextResource.toArray()[0].subject.valueOf();
+ sko.aboutResourceMap[nextId].about(newUri);
+ } else {
+ console.log("Error updating 1 resource for URI:"+nextUri+" in SKO about related node observer");
+ }
+ });
+ }
+ } else {
+ // @todo
+ console.log("!! this resource is now null, should be removed from list of resources?");
}
});
@@ -608,6 +616,8 @@ ko.bindingHandlers.about = {
// trace resources
sko.traceResources = function(rootNode, model, cb) {
+ console.log("** TRACING:");
+ console.log(rootNode);
var nodes = [];
if(jQuery(rootNode).attr("about") || jQuery(rootNode).attr("data-bind")) {
nodes.push(rootNode);
@@ -657,6 +667,7 @@ sko.traceResources = function(rootNode, model, cb) {
*/
sko.currentResource = function(node) {
console.log("in current resource");
+ console.log(node);
if(node == null) {
console.log("!!! top of DOM tree, About node not found");
return null;
@@ -678,13 +689,46 @@ sko.currentResource = function(node) {
}
};
+sko.currentResourceNode = function(node) {
+ console.log("in current resource node");
+ console.log(node);
+ if(node == null) {
+ console.log("!!! top of DOM tree, About node not found");
+ return null;
+ }
+ var aboutId = jQuery(node).attr('aboutId');
+ console.log("about id:"+aboutId);
+
+ if(aboutId) {
+ var uri = sko.about[aboutId]();
+ console.log("uri:"+uri);
+ if(uri != null) {
+ return node;
+ } else {
+ console.log("!!! current resource is null: "+aboutId);
+ return null;
+ }
+ } else {
+ console.log("recurring");
+ return sko.currentResourceNode(jQuery(node).parent().toArray()[0]);
+ }
+};
+
sko.traceRelations = function(rootNode, model, cb) {
+ console.log("*** TRACING RELATIONS:");
+ rootNode = (sko.currentResourceNode(rootNode) || rootNode);
+ console.log("*** FOUND ROOT NODE TO LOOK FOR RELATIONS:");
+ console.log(rootNode);
+
var nodes = [];
if(jQuery(rootNode).attr("rel") || jQuery(rootNode).attr("data-bind")) {
nodes.push(rootNode);
}
var childNodes = jQuery(rootNode).find("*[rel], *[data-bind]").toArray();
nodes = nodes.concat(childNodes);
+ console.log(" ** NODES TO LOOK FOR RELATED RESOURCES");
+ console.log(nodes);
+
var registerFn = function(k,env){
node = nodes[env._i];
var rel = jQuery(node).attr("rel");
@@ -15,7 +15,10 @@ ko.templateRewriting = (function () {
var tagToRetain = arguments[1];
var dataBindAttributeValue = arguments[6];
- dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
+ // @modified
+ // modified the rewritting function used
+ //dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(dataBindAttributeValue);
+ dataBindAttributeValue = ko.jsonExpressionRewriting.insertPropertyReaderWritersIntoJson(dataBindAttributeValue);
// For no obvious reason, Opera fails to evaluate dataBindAttributeValue unless it's wrapped in an additional anonymous function,
// even though Opera's built-in debugger can evaluate it anyway. No other browser requires this extra indirection.
@@ -28,8 +31,14 @@ ko.templateRewriting = (function () {
applyMemoizedBindingsToNextSibling: function (bindings) {
return ko.memoization.memoize(function (domNode, viewModel) {
- if (domNode.nextSibling)
- ko.applyBindingsToNode(domNode.nextSibling, bindings, viewModel);
+ if (domNode.nextSibling) {
+ // @modified
+ sko.traceResources(domNode.nextSibling, viewModel, function(){
+ sko.traceRelations(domNode.nextSibling, viewModel, function(){
+ ko.applyBindingsToNode(domNode.nextSibling, bindings, viewModel);
+ });
+ });
+ }
});
}
}
@@ -25,18 +25,21 @@
if ((typeof renderedNodesArray.length != "number") || (renderedNodesArray.length > 0 && typeof renderedNodesArray[0].nodeType != "number"))
throw "Template engine must return an array of DOM nodes";
- if (renderedNodesArray)
- ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
- ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode, [data]);
- });
-
+ // @modified
+ // Change the positoin of switch and if(render
+ // so the rendered node is added to the DOM before being unmemoized
switch (renderMode) {
case "replaceChildren": ko.utils.setDomNodeChildren(targetNodeOrNodeArray, renderedNodesArray); break;
case "replaceNode": ko.utils.replaceDomNodes(targetNodeOrNodeArray, renderedNodesArray); break;
case "ignoreTargetNode": break;
default: throw new Error("Unknown renderMode: " + renderMode);
}
+ if (renderedNodesArray)
+ ko.utils.arrayForEach(renderedNodesArray, function (renderedNode) {
+ ko.memoization.unmemoizeDomNodeAndDescendants(renderedNode, [data]);
+ });
+
if (options['afterRender'])
options['afterRender'](renderedNodesArray, data);
View
@@ -20,7 +20,7 @@
var test = function() {
- var daModel = {'currentResource': ko.observable("http://test.com/a")};
+ var daModel = {'currentResource': ko.observable("<http://test.com/a>")};
window['daModel'] = daModel;
console.log("*** initiating the PAGE");
sko.traceResources(jQuery("#to-trace")[0],daModel, function(){
View
@@ -0,0 +1,80 @@
+<html>
+<head>
+ <title>test semantic knockout.js</title>
+ <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
+ <script type='text/javascript' src='http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js'></script>
+ <script type='text/javascript' src="./deps/rdf_store.js"></script>
+ <script type='text/javascript' src="./build/output/knockout-latest.debug.js"></script>
+ <script type='text/javascript' src="./deps/sem_ko.js"></script>
+</head>
+<body>
+
+ <script type='text/javascript'>
+ $(document).ready(function(){
+
+ var testData = "INSERT DATA { <http://test.com/a> <http://test.com/title> 'test resource a' ; <http://test.com/count> '1'.\
+ <http://test.com/a> <http://test.com/related1> <http://test.com/c>.\
+ <http://test.com/a> <http://test.com/related2> <http://test.com/d>.\
+ <http://test.com/c> <http://test.com/title> 'test resource c' ; <http://test.com/count> '3' .\
+ <http://test.com/d> <http://test.com/title> 'test resource d' ; <http://test.com/count> '4' .\
+ <http://test.com/e> <http://test.com/title> 'test resource 3' ; <http://test.com/count> '2' .\
+ <http://test.com/e> <http://test.com/related1> <http://test.com/c>.\
+ <http://test.com/e> <http://test.com/related2> <http://test.com/d>. }";
+
+ var test = function() {
+
+ var daModel = {'currentResource': ko.observable("<http://test.com/a>"),
+ 'selectedTemplate': ko.observable('1')};
+ window['daModel'] = daModel;
+ console.log("*** initiating the PAGE");
+ sko.traceResources(jQuery("#to-trace")[0],daModel, function(){
+ console.log("*** TRACED RESOURCES!!!");
+ sko.traceRelations(jQuery("#to-trace")[0],daModel, function(){
+ console.log("*** TRACED RELATIONS!!!");
+
+ ko.applyBindings(daModel);
+
+ window['updateRootResource'] = function(newUri) {
+ sko.about['0'](newUri);
+ };
+
+ window['updateRelated'] = function(a,b) {
+ var query = "DELETE { "+a+" <http://test.com/related> ?o }";
+ query = query + " INSERT { "+a+" <http://test.com/related> "+b+" }";
+ query = query + " WHERE { "+a+" <http://test.com/related> ?o }";
+
+ };
+ });
+ });
+
+ };
+
+ sko.init(function(){
+ sko.store.execute(testData,
+ function(result, msg){
+ test();
+ });
+ });
+
+ });
+ </script>
+
+ <script id='test_template' type='text/html'>
+ {{if selectedTemplate() === '1'}}
+ <span id='the-related-thing' rel='<http://test.com/related1>'>
+ <p data-bind="text: <http://test.com/count>"></p>
+ </span>
+ {{else}}
+ <span id='the-related-thing' rel='<http://test.com/related2>'>
+ <p data-bind="text: <http://test.com/count>"></p>
+ </span>
+ {{/if}}
+ </script>
+
+ <div id="to-trace" about="currentResource">
+ <p data-bind="text: <http://test.com/title>"></p>
+ <span data-bind='template: "test_template"'></span>
+ </div>
+
+</body>
+</html>

0 comments on commit 642d4d4

Please sign in to comment.