我一直在尝试决定采用哪种方法来构建 UI:编程式还是声明式。我正在使用 JavaScript 和网络技术。在一些项目中,例如 ExtJS 和 YUI 他们采用了编程方法:
var container = new Ext.Container({
style: 'padding: 4px;',
items: [
new Button({text: 'hello'})
]
});
container.render(document.body);
当您需要动态创建视图组件时,编程方法可能是必要的(?)。它也可以更容易编程,至少对我来说是这样,因为我已经习惯了 API 和代码助手。
然而,一些技术,如 XUL(用于 Firefox 和 Thunderbird UI)是基于声明性方法:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="vbox example" title="Example 3...."
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<vbox>
<button id="yes" label="Yes"/>
<button id="no" label="No"/>
<button id="maybe" label="Maybe"/>
</vbox>
</window>
还有一个名为 AmpleSDK 采用声明式方法。因此,对于我的基于 JavaScript 的应用程序,我有两个选择可供选择。
Flex 还支持声明式 UI 以及编程式 UI 。 WFP 和 Silverlight 似乎专注于 XAML。另外,Open Laszlo 似乎使用声明性方法。
然后我看到 jQuery UI 是编程式的,并且 Dojo Toolkit 介于这两种方法之间。
我心中有几个问题:
1)动态(在运行时添加的组件)UI 是否可以仅以声明方式实现?
2)支持这两种方法是否有意义,以便可以在大部分部分使用声明式风格,并且如果需要,可以使用编程风格?
3)您更喜欢哪一个,为什么?
I have been trying to make a decision which approach to take to build UIs: programmatic or declarative. I am using JavaScript and web technologies. In some projects like ExtJS and YUI they have taken a programmatic approach:
var container = new Ext.Container({
style: 'padding: 4px;',
items: [
new Button({text: 'hello'})
]
});
container.render(document.body);
Programmatic approach may be a necessity when you need to create view components dynamically(?). It can also be easier to program, at least it feels that for me since I'm used to APIs and code assistants.
However, some technologies like XUL (used for Firefox and Thunderbird UI) are based on declarative approach:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="vbox example" title="Example 3...."
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<vbox>
<button id="yes" label="Yes"/>
<button id="no" label="No"/>
<button id="maybe" label="Maybe"/>
</vbox>
</window>
There is also a JavaScript GUI framework called AmpleSDK that takes the declarative approach. So, I am left with two choices to choose from, for my JavaScript based application.
Flex also supports declarative UIs, as well as programmatic ones. WFP and Silverlight seem to be focusing on XAML. Also, Open Laszlo seems to use the declarative approach.
Then I see jQuery UI being programmatic, and Dojo Toolkit being something in between the two approaches.
I have a few questions in my mind:
1) Can dynamic (components added in run-time) UIs be implemented solely in declarative manner?
2) Would it make sense to support both approaches, so that one could use declarative style for most of the part and if needed, use programmatic style?
3) Which one you prefer and why?
发布评论
评论(2)
1)是的,您始终可以执行以下操作: elem.innerHTML = "";大多数语言都支持类似的东西。
2)据我所知,大多数语言都支持两者。我的猜测是,最终,一种品种只是转变为另一种品种。如果不支持其中之一,语言就会限制自身。 (我不想使用 xyz,我无法以声明/编程方式编写 UI)。
3)我个人喜欢声明式,它往往可以用更少的行数完成事情。
1) Yes, you can always do something like: elem.innerHTML = "<content>"; Most languages support something similar.
2) From what I've seen most languages support both. My guess is in the end, one variety is just transformed into the other. By not supporting one or the other, the language would limit itself. (I don't want to use xyz, I can't write UI declaratively/programmatically).
3) I personally like the declarative, it tends to get things done in fewer lines.
这取决于您想要制作什么样的应用程序:
→ 静态
声明语法更好,更易于管理
→ 动态
声明式和程序式的混合将是最好的
It depends on what kind of application you want to make:
→ Static
Declarative syntax is much better, easier to manage
→ Dynamic
A mix of both declarative and programmatic will be best