我正在学习 xForms,但显然还不够好,因为我不明白为什么 这个代码 不起作用。
它在 FF2 中使用 xForms 扩展进行解析,但不呈现表单控件。 IE7 和 X-Smiles 给我带来了不同的问题,但我不确定这些问题是因为我的 xForms 还是其他原因 - 直到我让它在 FF2 中工作之前我真的无法判断。
I'm learning xForms, but apparently not well enough because I can't figure out why this code doesn't work.
It parses in FF2 w/ the xForms extension but does not render the form controls. IE7 and X-Smiles give me different problems, but I'm not sure if those problems are becaause of my xForms or because of something else- until I get it working in FF2 I can't really tell.
发布评论
评论(4)
不幸的是,本文档包含大量问题,我将依次讨论每个问题。
1) 最大的问题出现了几次,似乎源于模型和 UI 之间的一些混淆。 这两者在 XForms 中是完全独立的,它遵循模型-视图-控制器设计模式。 因此,您需要记住,模型中的所有内容与 UI 中的所有内容完全分开。 两者之间的关系很简单,UI 控件可以绑定到模型中的实例数据节点。 实际上,就您的文档而言,这意味着您的
select1
和repeat
元素不应该是model
元素的子元素。 只有instance
、bind
和action 元素可以是model
的子元素。2) 您正在使用多个
model
元素,这在如此简单的形式中是不必要的(因为每个model
可能包含许多instance
和绑定
)。 我之所以提出这一点,是因为您通过使用多个模型引入了一些潜在的陷阱,最好通过尽可能坚持使用一个模型来避免这些陷阱。 例如,instance
XPath 函数无法跨model
工作,因此您必须非常小心它们之间的数据依赖关系。 此外,UI 控件根据其绑定的model
进行刷新,这在过去当控件明显刷新不正常时经常给我带来问题。3) 您尝试使用
repeat
元素将子bind
应用到许多节点。 这是错误的,因为repeat
是 UI 元素,而不是模型元素。 但是,由于bind
采用nodeset
属性而不是ref
属性,因此您实际上不需要repeat
根本不。 相反,您可以这样做:4) 在许多 UI 控件上,您指定了一个 bind 属性和一个 ref 属性。 这些属性是互斥的,因为它们代表实现同一目标的不同方法。 ref 属性应包含标识要将 UI 控件绑定到的实例数据节点的 XPath。 bind 属性应包含已在其他地方定义的绑定元素的 id(在这种情况下,绑定元素本身将通过其节点集属性来标识控件绑定到的节点)。 因此,通过在同一个 UI 控件上使用这两个属性,您就会自相矛盾。
5) 在某些地方,您尝试使用 ref 属性将控件绑定到 UI 中的另一个元素。 控件只能绑定到实例数据。
6) 您在
repeat
中有一个setvalue
,您尝试在xforms-value-changed
事件上调用它。 此事件不会分派到repeat
元素,因此您的setvalue
将永远不会被调用。xforms-value-changed
事件仅分派到核心表单控件,即 在 XForms 规范中定义为:input|secret|textarea|output|upload|range|trigger|submit|select|select1
7) 此问题的另一个答案提到您将
model
元素放入文档正文中是错误的。 不幸的是,我没有足够的声誉来发表评论,但我只是想指出这个答案实际上是错误的。 尽管将model
元素放在文档head
中已成为惯例,但 XForms 规范中没有任何规定这样做。 事实上,一个主要的 XForms 处理器 Ubiquity XForms 实际上需要model
由于浏览器的限制,必须位于文档body
中。This document contains a ton of problems unfortunately, I'll go through each of them in turn.
1) The biggest problem occurs a few times and seems to stem from some confusion between the
model
and the UI. The two are entirely separate beasts in XForms, which adheres to the model-view-controller design pattern. So you need to remember that everything in themodel
is entirely separate from everything in the UI. The relationship between the two is simply that UI controls may bind to instance data nodes in yourmodel
s. Practically, in terms of your document, this means that yourselect1
andrepeat
elements should not be children ofmodel
elements. Onlyinstance
,bind
and the action elements may be children ofmodel
.2) You are using multiple
model
elements, which is unnecessary in such a simple form (because eachmodel
may contain manyinstance
s andbind
s). The reason I flag this is up is because you introduce a couple of potential pitfalls by using multiplemodel
s, which are best avoided by sticking to onemodel
where possible. For example, theinstance
XPath function will not work acrossmodel
s, so you have to be very careful about data dependencies between them. Also, a UI control is refreshed according to whichmodel
it is bound to, which has often caused me problems in the past when controls are apparently not refreshing sanely.3) You've tried to use a
repeat
element to apply a childbind
to many nodes. This is wrong becauserepeat
is a UI element, not a model element. However, sincebind
takes anodeset
attribute instead of aref
attribute, you don't actually need arepeat
at all. Instead, you can just do this:4) On many of your UI controls, you are specifying both a bind attribute and a ref attribute. These attributes are mutually exclusive, since they represent different ways to achieve the same thing. The ref attribute should contain XPath that identifies an instance data node that you want to bind the UI control to. The bind attribute should contain the id of a bind element that has been defined elsewhere (the bind element itself will identify the node that the control binds to in this case, via its nodeset attribute). So by using both attributes on the same UI control, you are contradicting yourself.
5) In some places, you've attempted to use a ref attribute to bind a control to another element in the UI. Controls may only be bound to instance data.
6) You have a
setvalue
inside arepeat
, which you are attempting to invoke on thexforms-value-changed
event. This event is not dispatched to therepeat
element, so yoursetvalue
will never be invoked. Thexforms-value-changed
event is only dispatched to the core form controls, which are defined in the XForms spec as:input|secret|textarea|output|upload|range|trigger|submit|select|select1
7) Another answer to this question mentions that you are wrong to put your
model
elements in the document body. Unfortunately I don't have enough reputation to comment there, but I just wanted to point out that answer is actually wrong. Although it has become conventional to put themodel
elements in the documenthead
, nothing in the XForms spec mandates this. In fact, one major XForms processor, Ubiquity XForms, actually requiresmodel
s to be in the documentbody
, because of browser limitations.您不应该将模型放置在主体部分。 相反,所有模型定义都应位于头部。 现在,您的代码不符合标准并且非常难以理解。
Xforms Wiki 书籍 是学习 XForms 的良好资源。
You shouldn't place your models in the body section. Instead all model definitions should be in the head section. As it is now, your code isn't standard compliant and is very difficult to understand.
Xforms Wiki book is a good resource for learning XForms.
我将添加 xf:repeat、xf:group、xf:input,... 不能是 xf:model 的子级
I'll add that xf:repeat, xf:group, xf:input,... can't be children of xf:model
XForms 验证器 是一个不错的起点。 之后,我建议从一个工作示例开始,逐渐添加代码,以观察哪个部分出现故障。
A good place to start could be an XForms validator. Afterwards, I'd recommend starting from a working example and adding your code gradually, to observe which part is failing.