Re: [Xsltforms-support] Setting node value in javascript
Brought to you by:
alain-couthures
From: Alain C. <ala...@ag...> - 2012-07-15 19:56:12
|
Mark, Setting a value with Javascript instructions is quite similar to using the setvalue action. Here is the Javascript code for this action: 1 XsltForms_setvalue.prototype.run = function(element, ctx) { 2 var node = this.binding.evaluate(ctx)[0]; 3 if (node) { 4 if (this.context) { 5 ctx = this.context.evaluate(ctx)[0]; 6 } 7 var value = this.value? XsltForms_globals.stringValue(this.context ? this.value.evaluate(ctx, ctx) : this.value.evaluate(node, ctx)) : this.literal; 8 XsltForms_globals.openAction(); 9 XsltForms_browser.setValue(node, value || ""); 10 document.getElementById(XsltForms_browser.getMeta(node.ownerDocument.documentElement, "model")).xfElement.addChange(node); 11 XsltForms_browser.debugConsole.write("Setvalue " + node.nodeName + " = " + value); 12 XsltForms_globals.closeAction(); 13 } 14 }; Only lines 8 to 12 are interesting for your situation: The "node" variable points to the XML node to be updated and the "value" variable contains its new text value. The refresh will be performed at the end with the "closeAction" method (line 12): the "openAction" method (line 8) is there to stack embedded actions. Depending on the DOM API of the browser, the value is assigned to the node (line 9). To limit the refresh according to dependencies, it is required to add the node to the changes list for its own model (line 10). Could you please test this with your form? For compatibility with future releases of XSLTForms, it would be better to define a method just for that, don't you think? Thank you for your feedbacks! -Alain Le 15/07/2012 21:24, Mark Seaborne a écrit : > Michael, > > Thanks for the suggestion, and I take your point. > > What I really need is a custom control. My JS function just takes a value chosen by a user when they select a feature on a map (openLayers vector layer), and pops the value into an XML instance. So, logically what I have is a select1 rendered as a map with selectable features. The features are already represented as XML (gml), rendered by openLayers as points on my map. > > So, yes, you are right, I am trying to do something with Javascript that I should be doing with just XForms! > > Best wishes > > Mark > > On 15 Jul 2012, at 19:46, C. M. Sperberg-McQueen wrote: > >> On Jul 15, 2012, at 5:50 AM, Mark Seaborne wrote: >> >>> Hi, >>> >>> I have noticed that if I change the form so that the js function is called from an XForms control using xf:load AND I add a bind to the model, then the XForms UI updates to reflect the value change caused by the js function. >>> >>> However, when the function is triggered elsewhere in the page, the UI is not updated, even with the bind statement in the model. >>> >> I have not yet studied any of the examples you sent, but stepping >> back a bit, at a very high level it looks as if you are doing (or: trying >> to do) things in Javascript that you could in theory do in XForms; >> making that work will always require that you know a lot about the >> internals of the XForms implementation. >> >> I wonder if you would make more headway doing less in JS and >> more in XForms -- define your function as a Javascript function that >> operates solely on its arguments and calculates a result, and >> register your function as an XPath function along the lines shown >> in http://en.wikibooks.org/wiki/XSLTForms/XSLTForms_only_Extensions#User-Defined_Functions >> -- then set the value of the node you want to update using the >> XForms setvalue action, and use XForms events to tie updates to >> the value to whatever triggering events are taking place elsewhere. >> >> Concretely, I don't think "Javascript function f has been invoked" >> or "... has completed execution" are events that appear in the XForms >> model. Various user actions, changes to values on specified nodes, >> and so on are events XForms knows about and I bet you'll have more >> luck getting XSLTForms to do what you want if you pose your problem >> in terms of those elements. >> >> Of course, if you've already got a lot of non-XForms Javascript in >> the app, this approach may seem impracticable. >> >> Just my two cents ... >> >> Michael Sperberg-McQueen >> >> -- >> **************************************************************** >> * C. M. Sperberg-McQueen, Black Mesa Technologies LLC >> * http://www.blackmesatech.com >> * http://cmsmcq.com/mib >> * http://balisage.net >> **************************************************************** >> >> >> >> > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Xsltforms-support mailing list > Xsl...@li... > https://lists.sourceforge.net/lists/listinfo/xsltforms-support > |