简体   繁体   English

主干JS:如何引导我的应用程序?

[英]Backbone JS: How do I bootstrap my application?

I am in the process of building my first really simple Backbone JS app, but I am struggling with a basic thing. 我正在构建我的第一个非常简单的Backbone JS应用程序,但是我正在努力解决一个基本问题。 While I have acquired a basic understanding of the Backbone JS view on MVC, I am wondering: How am I supposed to bootstrap my application? 当我对MVC上的Backbone JS视图有了基本的了解时,我想知道:我应该如何引导我的应用程序?

I've got all models, views and collections in place, but how do I bring it all together? 我已经准备好所有模型,视图和集合,但是如何将它们放在一起?

Of course, I have to instantiate my router: 当然,我必须实例化我的路由器:

new App.Router();
Backbone.history.start();

After that, I instantiate my main view: 之后,我实例化我的主要观点:

new App.Views.Main();

I've read that it's good practice to do this, inside it views should be loaded, events should be handled etc. But how am I supposed to do this? 我已经读到这样做是一种好习惯,应该在其中加载视图,应该处理事件等。但是我应该怎么做呢?

Doesn't this conflict with the so called best practice to bind stuff to events triggered by the router? 这与将东西绑定到路由器触发的事件的所谓最佳实践不冲突吗? I've also read this is the proper way to handle things: 我也读过这是处理事情的正确方法:

App.vent = _.extend({}, Backbone.Events);

App.Router = Backbone.Router.extend({
  routes: {
    '' : 'index'
  },
  index: function() {
    // Here, params passed to be main view can be passed along to other views
    App.vent.trigger('app:init');
  }
});

So how do I kick off my application properly? 那么如何正确启动我的应用程序?

The simple answer is: make your main view dependent of the router, and have the view listen to events from the router. 简单的答案是:使主视图依赖于路由器,并使该视图侦听来自路由器的事件。 Something along the lines of: 类似于以下内容:

var MyMain = Backbone.View.extend({
    initialize: function(options) {
        this.router = options.router;

        this.listenTo(this.router, 'some route event', this.myroutehandler);
    }

    myroutehandler: function(...) {
        // handle the routing
    }
});

var app = new MyMain({router: new MyRouter()});

Of course, you could invert this dependency by creating a router which is dependent of a view. 当然,您可以通过创建一个依赖于视图的路由器来逆转这种依赖关系。

There is not much wrong with making the objects dependent of each other, as long as you have a clear understanding of what their singular responsibility is. 只要您清楚了解对象的单一职责是什么,使对象彼此依赖并没有多大的错。 The main view may well be responsible for handling route events. 主视图很可能负责处理路线事件。 You may very well introduce another object responsible for tying the pieces together. 您可能会引入另一个负责将各部分绑在一起的对象。 That would be typically called a "front controller". 通常将其称为“前端控制器”。

Slightly better approach 更好的方法

However, usually it makes more sense to have a model which hold "state" of your application, and have the separate views listen to change events on that model, so they can act accordingly. 但是,通常更有意义的是,拥有一个模型来保存应用程序的“状态”,并让单独的视图侦听该模型上的更改事件,以便它们可以采取相应的行动。 This usually is the better approach, because it then no longer matters who or what changes the state, and you have the added benefit of having the model validate state transitions, ie, not navigating somewhere if something should prohibit the state from changing. 通常,这是一种更好的方法,因为这不再关系到谁或什么改变状态,而您还有一个额外的好处,就是让模型验证状态转换,即,如果某些事情应该禁止状态更改,则无需导航。

This model then listens to changes in the routing (whenever a user presses the back button or navigates somewhere) and whenever it's state gets updated from outside, changes the History state without firing events. 然后,该模型侦听路由中的更改(无论何时用户按下“后退”按钮或导航到某处),并且无论何时从外部更新其状态,都将更改“历史记录”状态而不会触发事件。 This way, your views no longer need to know about the router and/or history objects, which makes it more transparent who's responsible for changing history state and delegating the changes of this state throughout your application. 这样,您的视图就不再需要了解路由器和/或历史记录对象,这使得在整个应用程序中负责更改历史记录状态和委派此状态的更改的人员更加透明。

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

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