Flex 有组件。 HTML 中有什么相似之处?
Flex 中非常方便的一点是,您可以创建一个组件,对其进行编码,为其设置外观,然后您可以根据需要重用它。这意味着代码独立于您生成的每个副本。
我正在尝试使用 html 和 js 来实现这一点(我正在使用 jQuery,但对可能性持开放态度)。
基本上,我创建了一个页面(组件),它有自己的 JS 代码和自己的皮肤。现在,我想复制这个组件,并让每个组件都有自己的代码。
我尝试了两种选择。
- 将组件作为 iFrame 加载。这样,它会加载两个页面,从而隔离其中的代码。然而,iFrame 很麻烦。它们不能很好地进行拖放操作,并且在调整大小时表现不同。
- 我尝试使用 PHP 包含这些组件。然而,当我这样做时,我不知道如何隔离代码,因为它们都共享相同的源!换句话说,视觉部分被分割了(有两份),但下面的代码是相同的。
你知道有什么聪明的方法可以做到这一点吗?
如果您需要一个具体的例子,这里有一个简单的例子:
您正在创建一款汽车游戏。只有两辆车。每个玩家都使用相同的键盘并使用不同的键进行演奏。因此,您创建了一个“汽车”组件。每辆车都有相同的行为,但必须独立运行。
构建一个非常容易。但是如何在不重复代码的情况下构建第二个呢?
Something that is very handy in flex is that you can create a component, code it, skin it and then you can reuse it as you like. This means that the code is independent from each one of the copies that you produce.
I am trying to achieve this using html and js (I'm using jQuery, but open to possibilities).
Basically, I created a page (the component) that has it's own JS code and it's own skin. Now, I want to replicate this component and have each one with it's own code.
I tried two options.
- Have the components be loaded as an iFrame. This way, it loads two pages, therefore isolating the code within them. However, iFrames are a hassle. They dont work very well with drags and drops and behave differently when it comes to sizing.
- I have tried to include these components using PHP. However, when I do this, I can't figure out how to isolate the code because they all share the same source! In other words, the visual part is split (there are two copies), but the code underneath is the same.
Do you know of any smart way of doing this?
If you need a concrete example, here goes a simple one:
You are creating a car game. There are only two cars. Each player plays in the same keyboard with different keys. Therefore you create a "car" component". Each car has the same behavior but MUST run independently.
It's quite easy to build one. But how would you do the second one without duplicating the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能误解了很多,但你是这样谈论 OOP 的吗:
我不明白为什么这里需要单独的沙箱?但没有人回答,所以嗯。
I might be misunderstanding a whole lot but are you talking about OOP like so:
I don't see why here separate sandboxes are required? But nobody is answering so meh.
我不会创建单独的页面。页面是使用组件的上下文、应用程序。
Javascript 允许您创建类(不要与 CSS 类混淆)。您可以创建每个类的实例。因此,如果您有一个 Car 类,您可以实例化其中两个,并使它们与页面交互。例如:
现在您可以创建两辆车:
您还需要捕获钥匙。我不经常这样做,但可能有很多例子。
您可以将控制键作为汽车的属性,这样您就可以创建汽车并指定其钥匙。汽车可以告诉游戏它想要哪些钥匙。游戏可以处理所有输入并将其发送到正确的汽车。
之后,您将必须创建某种循环来添加动画。
当然,您也可以创建一个类来保存所有这些游戏信息。这样您就不会弄乱窗口对象(所有全局变量和函数实际上都会成为窗口对象的一部分),并且更容易将游戏嵌入到现有站点中。您甚至可以在一个页面上运行两个单独的游戏!尽管他们会为了钥匙而打架。
不管怎样,还有很多空白需要填补,但我希望这能给你一个想法。
I'd not create separate pages. The page is the context, the application, in which the components are used.
Javascript allows you to create classes (not to be confused with CSS classes). You can create instances of each class. So if you got a class Car, you can instantiate two of those, and make them interact with the page. For instance:
Now you can create two cars:
You will also need to capture keys. I haven't done that often, but there are probably numerous examples.
You can make the control-keys a property of the car, so you can create a car and specify its keys. The car can tell the game which keys it wants. The game can handle all input and send it to the right car.
After that, you will have to create a loop of some sort to add animation.
Of course you can create a class as well to hold all this game information. That way you don't clutter your window object (all global variables and functions will actually become part of the window object), and it will be easier to embed the game in an existing site. You could run even two separate games in a single page! Although they would fight over the keys.
Anyway, there are lots of gaps to fill, but I hope this gives you an idea.