简体   繁体   English

我是否以正确的方式构建我的应用程序? - Backbone.js

[英]Am I structuring my app the right way? - Backbone.js

I am very new to Javascript and backbone.js. Have developed an application in AS3 using RobotLegs and need to port it to Javascipt.我是 Javascript 和 backbone.js 的新手。使用 RobotLegs 在 AS3 中开发了一个应用程序,需要将其移植到 Javascipt。 For the client side MVC, I started looking at Backbone.js. The concepts seem straightforward but I'm a little unsure about doing things the "right" way.对于客户端 MVC,我开始查看 Backbone.js。这些概念看起来很简单,但我不确定是否以“正确”的方式做事。 Also have some places I'm stuck...还有一些地方我卡住了......

So basically, I have this web service (non RESTful) that I need to GET a JSON object with the apps config from.所以基本上,我有这个 web 服务(非 RESTful),我需要使用来自的应用程序配置获取 JSON object。 Ideally, I think the best way to make this call would be from a Backbone model, but I'm not sure how to override the URL and Parse method to make it work with Non Restful services.理想情况下,我认为进行此调用的最佳方式是从 Backbone model 进行调用,但我不确定如何覆盖 URL 和 Parse 方法以使其与非 Restful 服务一起使用。 So for now I'm making an Ajax call to it from outside of the Model in App.js (code shared below).所以现在我从 App.js 中的 Model 外部调用 Ajax(下面共享代码)。

Once I get the data from the JSON call, I want to store it in a ConfigModel (SingleTon Model) object that I can query from modules to retrieve value objects that eventually populate some views.从 JSON 调用中获取数据后,我想将其存储在 ConfigModel(SingleTon 模型)object 中,我可以从模块中查询以检索最终填充某些视图的值对象。 I have 2 issues here.我这里有两个问题。

1) I don't know how to implement a Singleton Model using Backbone.js 2) I don't know how I can divide my ConfigModel further into smaller models/collections and if I need to make them Singleton too or just pass them to the View constructors. 1) 我不知道如何使用 Backbone.js 实现 Singleton Model 2) 我不知道如何将我的 ConfigModel 进一步划分为更小的模型/集合,如果我也需要将它们制作成 Singleton 或者只是将它们传递给视图构造函数。

I'm using Require.js to define([]) the dependent modules for a particular view/model etc. Now I'm not sure about the scope of the loaded modules.我正在使用 Require.js 来定义([])特定视图/模型等的依赖模块。现在我不确定加载模块的 scope。 If I need to make them Singleton then I should just load them once somewhere and then inject a dependency into other classes/module objects.如果我需要使它们成为 Singleton,那么我应该只在某处加载它们一次,然后将依赖项注入到其他类/模块对象中。 How do I carry this out with Backbone.js???我如何使用 Backbone.js 执行此操作???

Lastly, I want to use custom events throughout the framework.. Sort of Framework events that I can dispatch globally and listen to from Models/Views etc...最后,我想在整个框架中使用自定义事件……我可以在全球范围内分发并从模型/视图等中收听的框架事件……

As you can see, I'm pretty confused regarding scopes/singletons and how all of this fits in with Backbone.js & Require.如您所见,我对作用域/单例以及所有这些如何适应 Backbone.js 和 Require 感到很困惑。

Here's how I have my app structured as of now...这是我现在如何构建我的应用程序......

1) My index.html file loads up "Main.js" with Require.js 1) 我的 index.html 文件用 Require.js 加载“Main.js”

2) In Main.js I load up App.js and call init on it. 2) 在 Main.js 中,我加载 App.js 并对其调用 init。 Init calls a getConfig() function which makes an AJAX call to the web service and gets and parses a JSON text file. Init 调用 getConfig() function,它对 web 服务进行 AJAX 调用,并获取并解析 JSON 文本文件。 This I wanted to store in the singleton ConfigModel.我想将其存储在 singleton ConfigModel 中。 However, I have that commented right now because I started playing around with ways to make the webservice call from with my ConfigModel itself... Which Didn't work.但是,我现在有评论,因为我开始尝试使用我的 ConfigModel 本身进行 web 服务调用的方法......这没有用。

3) Then I have a router with a default URL map. I initialize the Router after succesfully receiving the JSON from the web service. 3)然后我有一个默认URL map的路由器。我从web服务成功接收到JSON后初始化路由器。 I'm not sure if this is correct or I should make the Ajax call from the Router?我不确定这是否正确,或者我应该从路由器拨打 Ajax 电话?

Here's the code: http://pastebin.com/HHRF88Tq这是代码: http://pastebin.com/HHRF88Tq

I know I'm all over the place, but if someone could guide me in the right direction, I'd really appreciate it....我知道我到处都是,但如果有人能指导我朝着正确的方向前进,我将非常感激......

Thanks a bunch.非常感谢。

Sameer萨米尔

You wanna look at backbone patterns for best practices ( like bootstrapping your models ! )您想查看骨干模式以获得最佳实践(例如引导您的模型!)

http://ricostacruz.com/backbone-patterns/ http://ricostacruz.com/backbone-patterns/

and checkout backbone fundamentals for aa complete overview on backbone architecture ( wip )并检查骨干基础知识以获得骨干架构的完整概述(wip)

http://addyosmani.github.com/backbone-fundamentals/ http://addyosmani.github.com/backbone-fundamentals/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM