当我的视图/控制器需要位于同一个文件中时,构建 js 应用程序的最佳方法是什么

发布于 2024-11-25 23:45:30 字数 384 浏览 0 评论 0原文

我正在构建一个学习应用程序,其中有许多不同的页面类型供学习者浏览并进行活动。它将是一个符合 SCORM 的学习对象。

这是我到目前为止的结构...

application/
  models/
    scorm.js
    sequence.js
    session.js

  pagetypes/
    multichoice.js
    truefalse.js
    basic.js

  utilities/
    jquery.js
    api.js

我的页面类型进行查看和控制,我应该将它们分开吗?我将它们组合起来的原因是,当我构建新的页面类型时,我可以将其放入该文件夹中,代码会立即识别它。

你们觉得怎么样?氨基甙?

I am Building a learning application where there are a bunch of different page types that a learner will go through and do activities. It will be a SCORM compliant learning object.

This is the structure I have so far...

application/
  models/
    scorm.js
    sequence.js
    session.js

  pagetypes/
    multichoice.js
    truefalse.js
    basic.js

  utilities/
    jquery.js
    api.js

My pagetypes do the viewing and the controlling, should I seperate these out? The reason I have combined them is so when I build a new page type, I can just drop it into that folder and it will get recognised straight away by the code.

What do you guys think? amidoinrite?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-12-02 23:45:30

我猜您正在根据页面交互的类型来分离方法。

我看不出有什么理由不按照你的方式去做。只要 sco 需要的所有内容都在清单中,您就可以根据需要细分脚本。分离出单独的页面类型可能会节省一点加载时间...但前提是您只将需要的内容加载到 HTML 页面中,&您实际上是在 sco 会话中浏览页面。如果您要将所有脚本加载到单个 HTML 页面中,&然后动态改变页面div的内容,然后你的脚本全部加载1次&您也可以为所有页​​面类型脚本创建 1 个缩小文件。

我可能会选择后者,&将交互与标记中的类或 ID 联系起来。 1 个文件,缩小工作量更少,&我可以在其他包中使用,而不必确保我拥有我需要的每种页面类型......

I'm guessing you're separating out methods based on type of page interactions.

I don't see any reason not to do it your way. So long as everything the sco needs is in the manifest you can subdivide your scripts however you want. It might save just a bit of load time to separate out separate page types... But only if you are only loading what you need into the HTML page, & you are actually navigating pages within a sco session. If you're loading all script into a single HTML page, & then dynamically changing the content of page divs, then your scripts are all loaded 1 time & you may as well have 1 minified file for all page type scripts.

I would probably go with the latter, & tie interactions to classes or ids in the markup. 1 file, less work to minify, & I can use in other packages without having to make sure that I have every page type I need...

朕就是辣么酷 2024-12-02 23:45:30

对于 JavaScript,将其分开可能很棘手,因为它与视图非常接近。只要数据与实际视图分离(看起来就像在您的示例中),这将是一个很好的设计。我认为页面类型更多的是控制器,而 HTML 是视图。最重要的部分是保持模型与视图分离。除非您尝试构建可重用的 JavaScript/HTML 组件,否则页面类型可以模糊控制器和视图的角色。

With JavaScript it can be tricky to separate it out since it lives so closely to the view. As long as the data is separated from the actual view (which it looks like it is in your example) it will be a good design. I would argue that the pagetypes are more controllers and the HTML is the view. The most important part is to keep the model separated from the view. Unless you're trying to build reusable JavaScript/HTML components it's ok for pagetypes to blur the role of controller and view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文