BackboneJS 站点结构
好吧,我正在尝试了解整个 backboneJS 事情。我知道您必须将站点分成模块,并将每个模块分解为模型、集合和视图,如 此示例。
我的 JS 文件结构目前如下所示:
-js
-application.js
-lib
-jquery.min.js
-backbone.min.js
-underscore.min.js
-modules
-newsfeed.js //activity feed
-file.js // page to upload files to
-members.js // page that show other members of group
//-general-site-logic.js??
我有两个问题:
是否应该从 BackboneJS?如果不是,那么这个单独的逻辑应该驻留在我的应用程序结构中的哪里?当然,骨干网无法控制所有客户端活动。不涉及任何集合的活动怎么样?
在使用 BackboneJS 时是否应该使用 RequireJS 来管理模块?我找到了 这个示例,但它似乎使已经令人困惑的 Backbone 概念变得复杂甚至更进一步。
我即将开始开发一个 javascript 非常重的应用程序,并且真的希望在我的代码开始增长之前把它做好!
Ok I am trying to get my head round this whole backboneJS thing. I understand you have to separate your site into modules and break each module down into Models, Collections and Views like described in this example.
My JS file structure currently looks like this:
-js
-application.js
-lib
-jquery.min.js
-backbone.min.js
-underscore.min.js
-modules
-newsfeed.js //activity feed
-file.js // page to upload files to
-members.js // page that show other members of group
//-general-site-logic.js??
I have two questions:
Should all application logic be controlled from BackboneJS? If not then where should this separate logic reside in my application structure? Surely backbone can't control all of your client-side activity. What about activity that doesn't involve any collections?
Should I be using RequireJS to manage modules when using BackboneJS or not? I have found this example but it seems to complicate the already confusing concepts of Backbone even further.
I am about to embark on a very javascript heavy app and really want to get this right before my code begins to mushroom!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Backbone 的伟大之处在于,它只是有用的部分的集合,您可以根据需要将它们组合在一起。您可以随心所欲地组织它。
为什么不呢?我有一个相当大的客户端应用程序,其中所有代码(除了 jQuery 插件等)都是使用 Backbone 构造(视图、模型、集合、路由器)编写的。
在我们的例子中,我们使用 Rails,所以我们不需要担心需要其他 JS 文件。我们将项目分解为许多 js(咖啡)文件,“资产管道”为我们将其全部合并到一个 js 文件中。 (我们确实需要告诉资产管道一些排序规则,但是......模型在集合之前,集合在视图之前,等等)
当我们这样做时,我们有以下设置:
当然,这就是我们这样做的方式。对于较大的项目,我总是知道在哪里可以找到我的组件,并且我们为不同的子视图创建子文件夹。例如:
但是,在较小的项目中,您可以将所有内容放入一个 JS 文件中,这不会成为问题。大多数玩具示例都是这样布置的。
中间布局可能是您将模型与视图分开的东西,如下所示:
我想您应该从中得到的是:“按照您想要的方式组织”。做对项目规模和团队对组织的理解有意义的事情。
主干提供结构。然而,对于该结构的设计方式并没有固执己见。
The great thing about Backbone is that it is just a collection of useful pieces that you can put together however you want. You can organize it however you want.
Why not? I have a rather large client-side app where all of the code (aside from jQuery plug-ins and such) is written using Backbone constructs (Views, Models, Collections, Routers).
In our case, we are using Rails, so we don't need to worry about requiring other JS files. We break the project up into many js (coffee) files and the "asset pipeline" merges it all into one js file for us. (we do need to tell the asset pipeline some ordering rules, however... models before collections, collections before views, etc)
When we do this, we have the following setup:
Of course, that is how WE do it. For larger projects, I always know where to find my components and we create subfolders within for our different sub-views. For instance:
On smaller projects, however, you can put everything in one JS file and it wouldn't be a problem. Most toy examples are laid out this way.
An intermediate layout might be something where you just separate your models from your views like this:
I guess what you should get from this is: "Organize however you'd like". Do what makes sense for the project size and your team's understanding of organization.
Backbone provides structure. It isn't opinionated, however, to how that structure is designed.
如果有帮助的话,我有一个 bootstrap 项目入门集成 backbone.js、coffeescript、sinatra、jasmine 和 sketch。
它将帮助您开始了解项目结构,并节省您集成技术堆栈的时间。还使用骨架 css 进行响应式设计。
If it helps I have a bootstrap, project starter integrating backbone.js, coffeescript, sinatra, jasmine and skeleton.
It'll get you started with project structure and save you time integrating the tech stack. Also uses skeleton css for responsive design.