如何使用knockout.js制作单页应用程序?

发布于 2024-12-25 06:22:58 字数 124 浏览 0 评论 0原文

如何组织视图模型类?单独的类还是一个巨大的类(以及如何模块化这个巨大的类)?如何在“页面”之间切换(当然使用ajax加载数据)?页面加载后如何加载backbonejs的模板?只要示例和教程比其网站上的示例和教程更先进,我们将不胜感激。

How do you organize the view-model classes? Separate classes or one giant one (and how to modularize the giant one)? How to switch between 'pages' (with data loaded with ajax of course)? How to load templates for backbonejs after page load? Examples and tutorials would be appreciated as long as they are more advanced that he ones on their site.

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

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

发布评论

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

评论(1

梦明 2025-01-01 06:22:58

哇,很多问题都集中在一个问题上。我会尝试在这里击中它们,但免责声明是我正在为 Pluralsight 编写关于 Knockout 的整个课程......所以我无法深入探讨您需要评论框的深度:)

1)您如何组织模型- 查看课程?

我认为你的意思是视图模型类。我建议创建一个包含页面所需的所有属性和方法的视图模型。将视图模型放在一个附件中,并将所有 javascript 保存在自己的文件中。例如,如果您的结构有页面customers.html,那么您的视图模型就可以有customers.js。

2)单独的类还是一个巨大的类(以及如何模块化这个巨大的类)?

每个视图模型都在其自己的文件中。通常每个 js 文件有 1 个视图模型(但您当然可以将相互关联的视图模型放入同一文件中)。另外,通常每个视图有 1 个视图模型(但同样,在某些情况下您可以超越此范围)。

对于模型,如果您愿意,您可以将它们全部创建在一个模型文件中,或者将它们放在单独的文件中。对于较小的应用程序,我喜欢 1 个 models.js 文件,因为大多数模型都非常简单、较小,并且包含简单的属性(尽管是可观察的)。但在这里,这确实是一个选择的问题。

3)如何进行页面切换?

该机制并非 Knockout 特有的,因此您可以使用链接甚至自定义控件(菜单、选项卡等)。你的电话。一旦您决定如何导航到不同的页面,我假设您需要在 2 个页面之间传递数据作为上下文,一种方法是在带有 ID 的查询字符串中,另一种方法是本地存储(对于较大的项目,您需要存储在客户),或者还有其他选择。同样,这实际上取决于您需要在页面之间传递什么。很多时候,我将页面设计为自给自足,因此我不需要在它们之间传递太多内容。对于我需要传递的内容,我尝试发送尽可能少的内容,并根据键查找数据(在 ajax 调用或本地存储中......无论数据位于何处)。

我希望这会有所帮助。

更新:这是我在 Pluralsight 上提到的 Knockout.js 课程的链接

Wow, lots of questions all rolled into one. I'll try to hit them here, but a disclaimer that I am writing an entire course for Pluralsight on Knockout ... so i can't go into the depth you need an a comment box :)

1) How do you organize model-view classes?

I think you mean view-model classes. I recommend creating a view model that contains all of the properties and methods that the page requires. Put the view model in an enclosure and keep all of that javascript in its own file. For example, if you have page customers.html for your structure, you could have customers.js for with your view model.

2) Separate classes or one giant one (and how to modularize the giant one)?

Each view model is in its own file. Generally 1 view model per js file (but you could certainly put interrelated ones int he same file). Also, generally 1 view model per view (but again, you can go beyond this in some cases).

For Models you can create them all in a single models file if you like, or put them in separate files. For smaller apps I like 1 models.js files since most models are pretty straight forward, small, and contain simple properties (albeit observables). But here, it really is a matter of choice.

3) How to switch between pages?

The mechanism is not specific to Knockout, so you can use links or even custom controls (menus, tabs, etc). Your call. Once you decide how to navigate to a different page, I assume you need to pass data between the 2 for context One way to do this is in the querystring with an ID, another way is local storage (for larger items you need to store in the client), or there are other options too. Again, it really depends on what you need to pass between the pages. A lot of times I design the pages to be self sufficient so I dont need to pass much between them. For what I need to pass, i try to send as little as I can and look up data based on keys (in ajax calls or local storage ... wherever the data lives).

I hope this helps a bit.

UPDATE: Here is the link I mentioned to my Knockout.js course on Pluralsight

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