简体   繁体   English

如何使用Knockout构建单页面应用程序?

[英]How do I architect single page application using Knockout?

I have begun to play with Knockout recently and I must say it is truly awesome and the power that it can bring to web applications. 我最近开始玩Knockout,我必须说它真的很棒,它可以为Web应用程序带来的力量。

However now I want to do something real world and architect my solution using Knockout. 但是现在我想做一些现实世界并使用Knockout构建我的解决方案。 Eg. 例如。 How should my viewmodels be initialized. 我应该如何初始化我的视图模型。 Where should they go? 他们应该去哪儿? I am mainly targeting single page application. 我主要针对单页面应用程序。 So, I am interested in architecting single page applications mainly. 所以,我主要对构建单页面应用程序感兴趣。

There are a lot of pieces to the puzzle, but here is a short list for me. 拼图有很多部分,但这里有一个简短的清单。

DISCLAIMER: I'll make some assumptions about your app too, so some of it will vary. 免责声明:我也会对您的应用做出一些假设,因此其中一些会有所不同。 Also, this is just one way to do it. 此外,这只是一种方法。 There are may good ways. 可能有很好的方法。 But this should be a good starting point for you. 但这应该是一个很好的起点。

Assuming your app is a set of abut 5 main views in a SPA: 假设您的应用程序是一组SPA中的5个主视图:

  1. Create a master/shell html page to house the app 创建一个主/ shell html页面来存放应用程序
  2. Create a view / partial page (html) for each view. 为每个视图创建视图/部分页面(html)。 Each view is hidden til you navigate/route to it. 每个视图都被隐藏,直到您导航/路由到它。
  3. Create a viewmodel that can be bound to each view Create a bootstrapper.js that kickstarts everything. 创建一个可以绑定到每个视图的viewmodel创建一个bootstrapper.js,用于启动所有内容。
  4. Bootstrapper should crank up any routing engine you use (sammy, history, etc) Bootstrapper应该启动你使用的任何路由引擎(sammy,history等)
  5. Bootstrapper will bind the views to the viewmodels Bootstrapper can also crank up any seed data and state for your SPA Bootstrapper将视图绑定到视图模型Bootstrapper还可以为您的SPA启动任何种子数据和状态
  6. Tip: Use SoC. 提示:使用SoC。 Have your viewmodels be models for views. 让您的视图模型成为视图的模型。 Don;t have them do routing, ajax calls, ui manipulation, etc. Have separate objects for other functions. 不要让他们做路由,ajax调用,ui操作等。为其他功能分别拥有对象。 DRY, KIS, SoC ... all good stuff :-) DRY,KIS,SoC ......所有好东西:-)

I use the Revealing Module Pattern for creation of my viewmodels, though standard Module is perfectly fine too. 我使用Revealing Module Pattern来创建我的视图模型,尽管标准模块也非常好。

If you ave a more specific question, happy to answer that. 如果你有一个更具体的问题,很高兴回答这个问题。 I tried to keep this short cuz in truth it's not a 5 minute answer. 我试着保持这个简短的事实,这不是一个5分钟的答案。 In fact, I'm writing a course for Pluralsight on one way to do this coming in August 2012 :-) 事实上,我正在为Pluralsight写一篇关于2012年8月这样做的方法:-)

I just open-sourced the mini SPA framework I put together with Knockout being the major component. 我只是开源了我将Knockout作为主要组件放在一起的迷你SPA框架。

knockout-spa A mini (but full-fledged) SPA framework built on top of Knockout, Require, Director, Sugar. knockout-spa一个迷你(但功能齐全的)SPA框架,建立在Knockout,Require,Director,Sugar之上。 https://github.com/onlyurei/knockout-spa https://github.com/onlyurei/knockout-spa

Live Demo: http://knockout-spa.mybluemix.net 现场演示: http //knockout-spa.mybluemix.net

Features 特征

  • Routing (based on Flatiron's Director): HTML5 history (pushState) or hash. 路由(基于Flatiron的Director):HTML5历史(pushState)或哈希。
  • Highly composable and reusable: pick modules/components for a page in the page-specific JS and they will be auto-wired for the page's HTML template 高度可组合和可重用:在页面特定的JS中为页面选择模块/组件,它们将自动连接到页面的HTML模板
  • SEO ready (prerender.io) SEO准备好了(prerender.io)
  • Fast and lightweight (85 KB of JS minified and gizpped) 快速轻巧(85 KB的JS缩小和gizpped)
  • Two-tier bundle build for JS for production: common module that will be used by most pages, and page-specific modules that will be lazy-loaded 用于生产的JS的双层捆绑构建:大多数页面将使用的公共模块,以及将延迟加载的特定于页面的模块
  • Organized folder structure to help you stay sane for organizing and reusing JS, CSS, HTML 有组织的文件夹结构,帮助您保持理智,组织和重用JS,CSS,HTML
  • Using Knockout 3.3.0+ so ready for Knockout's flavor of web component and custom tags ( http://knockoutjs.com/documentation/component-overview.html ) 使用Knockout 3.3.0+为Knockout的Web组件和自定义标签做好准备( http://knockoutjs.com/documentation/component-overview.html
  • All documentation are in the major dependencies' own homepages, so that you don't need to completely learn a new framework 所有文档都在主要依赖项的主页中,因此您无需完全学习新的框架

For those who need an updated alternative (2015)... Another option (and a very good one) is to use Yeoman to scaffold a single-page-app for you as described in Steven Sanderson's blog here 对于那些谁需要更新的备选方案(2015年)......另一种选择(和一个很好的一个)是使用约曼在史蒂芬·桑德森的博客中描述脚手架单页面应用为你在这里

It does all the architecturing you need for you so you can concentrate on writing code. 它完成了您需要的所有架构,因此您可以专注于编写代码。 Try best to harness the re-usability of the knew knockout component feature - it's amazingly described by Steve Sanderson so I won't take away the glory of his well done work. 尽量利用已知淘汰组件功能的可重用性 - 史蒂夫桑德森精心描述这一点,所以我不会带走他干得好的作品的荣耀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用导航栏创建单页面应用程序? - How do I create single page application using a navbar? 您将如何在单页面应用程序和knockout.js中进行客户端验证 - How would you do client side validation in single page application and knockout.js 使用单页面应用程序中的knockout绑定导航id - navigate with id using knockout binding in single page application 如何在单页应用程序 (SPA) 中实现 ReCaptcha - How do I implement ReCaptcha in a Single Page Application (SPA) 如何使用mvc重定向到应用程序中的另一个页面 - How do I redirect to another page in my application using mvc 如何使用 Javascript 访问这些敲除伪变量? - How do I access these knockout pseudo variables using Javascript? 如何使用 knockout 设置输入标签的大小和值? - how do i set the size and the value of an input tag using knockout? 如何使用 knockout 绑定新元素? - How do I bind new elements using knockout? 如何在Rails 2.3应用程序的单个页面上替换原型? - How do I replace prototype on a single page of my rails 2.3 application? 如何编写将内容脚本应用于单页应用程序的 chrome 扩展? - How do I write a chrome extension that applies a content script to a Single Page Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM