<bdo id='rrreS'></bdo><ul id='rrreS'></ul>

  1. <small id='rrreS'></small><noframes id='rrreS'>

  2. <legend id='rrreS'><style id='rrreS'><dir id='rrreS'><q id='rrreS'></q></dir></style></legend>
    <i id='rrreS'><tr id='rrreS'><dt id='rrreS'><q id='rrreS'><span id='rrreS'><b id='rrreS'><form id='rrreS'><ins id='rrreS'></ins><ul id='rrreS'></ul><sub id='rrreS'></sub></form><legend id='rrreS'></legend><bdo id='rrreS'><pre id='rrreS'><center id='rrreS'></center></pre></bdo></b><th id='rrreS'></th></span></q></dt></tr></i><div id='rrreS'><tfoot id='rrreS'></tfoot><dl id='rrreS'><fieldset id='rrreS'></fieldset></dl></div>

      <tfoot id='rrreS'></tfoot>
    1. 引导 Backbone 应用程序

      Bootstrapping a Backbone application(引导 Backbone 应用程序)
          <tbody id='0U8V5'></tbody>
        • <small id='0U8V5'></small><noframes id='0U8V5'>

          <legend id='0U8V5'><style id='0U8V5'><dir id='0U8V5'><q id='0U8V5'></q></dir></style></legend>
        • <tfoot id='0U8V5'></tfoot>

            <i id='0U8V5'><tr id='0U8V5'><dt id='0U8V5'><q id='0U8V5'><span id='0U8V5'><b id='0U8V5'><form id='0U8V5'><ins id='0U8V5'></ins><ul id='0U8V5'></ul><sub id='0U8V5'></sub></form><legend id='0U8V5'></legend><bdo id='0U8V5'><pre id='0U8V5'><center id='0U8V5'></center></pre></bdo></b><th id='0U8V5'></th></span></q></dt></tr></i><div id='0U8V5'><tfoot id='0U8V5'></tfoot><dl id='0U8V5'><fieldset id='0U8V5'></fieldset></dl></div>
              • <bdo id='0U8V5'></bdo><ul id='0U8V5'></ul>

                本文介绍了引导 Backbone 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我已经看到了几种方法来做到这一点,但我永远无法弄清楚哪种方法是正确"的.

                I have seen a couple of ways on how to do this, but I can never figure out which is the 'correct' way.

                来自 NetTuts+ 的 Jeffrey Way 和 Addy Osmani 实例化一个主"应用程序查看以启动他们的应用程序.

                Jeffrey Way from NetTuts+ and Addy Osmani instantiate a 'main' application view in order to kick off their applications.

                require(['views/app'], function(AppView) {
                  new AppView();
                });
                

                Railscasts 的 Ryan Bates 通过实例化一个然后处理后续请求的路由器:

                Ryan Bates from Railscasts starts his application by instantiating a router which then handles subsequent requests:

                window.App =
                    Models: {}
                    Collections: {}
                    Views: {}
                    Routers: {}
                
                    init: ->
                        new App.Router()
                        Backbone.history.start()
                    }
                }
                
                $(document).ready ->
                    App.init()
                

                这两种引导应用程序的方式之间是否有重要区别?

                Is there an important difference between these two ways of bootstrapping an application?

                我非常喜欢 Ryan Bates 如何创建一个 App 对象,他将所有模型、视图都附加到该对象上……虽然他使用的是 CoffeeScript,但我不确定这是否会对这个对象得到处理.我试过了,但我无法让它与 RequireJS 一起工作:

                I quite like how Ryan Bates creates an App object to which he attaches all his models, views, ... He uses CoffeeScript though, I'm not sure if this makes any difference in how this object gets handled. I tried this and I couldn't get it to work with RequireJS:

                require(['jquery', 'backbone', 'router'], function ($, Backbone, Router) {
                    window.App = {
                        Models: {},
                        Collections: {},
                        Views: {},
                        Aggregator: _.extend({}, Backbone.Events),
                        Hook: $('#application'),
                        Router: Router,
                
                        init: function() {
                            new App.Router();
                            Backbone.history.start();
                        }
                    }
                    $(document)ready(function() {
                        App.init();
                    });
                });
                

                然后我有一个简单的路由器,它在索引路由被命中时创建一个 loginView:

                I then have a simple router which creates a loginView when the index route gets hit:

                define(['backbone', 'loginView'], function(Backbone, LoginView) {
                  var Router = Backbone.Router.extend({
                
                    routes: {
                      '': 'index'
                    },
                
                    index: function() {
                      var loginView = new LoginView();
                    }  
                
                  });
                
                  return Router;
                });
                

                还有我的登录视图:

                define(['backbone'], function(Backbone) {
                  var LoginView = Backbone.View.extend({
                
                  });
                
                
                
                  return LoginView;
                });
                

                为了遵循 Ryan Bates 的工作方式,我想做一些类似的事情:

                To follow Ryan Bates' way of working, I wanted to do something like:

                App.Views.LoginView = Backbone.View.extend({});
                

                但我不太确定这与他在咖啡脚本中所做的有什么不同:

                but I'm not quite sure how this differs from what he does in coffeescript:

                class App.Views.LoginView extends Backbone.View
                

                当我在 LoginView 的 initialize 方法中将App"记录到控制台时,我会从 main.js 文件中获取对象,但是,当我尝试将某些内容附加到 App.Views 对象时,它说 App.Views 是未定义的.一定是这里做错了什么?

                When I log 'App' to the console in my LoginView's initialize method, I get the object from my main.js file, however, when I try to attach something to the App.Views object, it says App.Views is undefined. Must be doing something wrong here?

                推荐答案

                创建时:

                App.Views.LoginView = Backbone.View.extend({});
                

                与以下完全不同:

                class App.Views.LoginView extends Backbone.View
                

                您可以通过将咖啡脚本从 coffee 转换为 js 来检查咖啡脚本:

                You can check the coffeescript by converting it from coffee to js:

                var __hasProp = {}.hasOwnProperty,
                __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
                
                App.Views.LoginView = (function(_super) {
                
                  __extends(LoginView, _super);
                
                  function LoginView() {
                    return LoginView.__super__.constructor.apply(this, arguments);
                  }
                
                return LoginView;
                
                })(Backbone.View);
                

                我建议查看 todomvc 的主干的 repo-需要设置.

                我有一个咖啡脚本 todo setup 松散地基于这两个全局应用程序对象不附加到窗口,而是使用子对象来保存集合、模型、视图等.

                I have a coffeescript todo setup based loosely on both with a global app object not attached to the window, but using sub objects to hold collections, models, views etc.

                这篇关于引导 Backbone 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  <tbody id='BlfUQ'></tbody>

                  <legend id='BlfUQ'><style id='BlfUQ'><dir id='BlfUQ'><q id='BlfUQ'></q></dir></style></legend>

                      <small id='BlfUQ'></small><noframes id='BlfUQ'>

                        <bdo id='BlfUQ'></bdo><ul id='BlfUQ'></ul>
                        <tfoot id='BlfUQ'></tfoot>
                      • <i id='BlfUQ'><tr id='BlfUQ'><dt id='BlfUQ'><q id='BlfUQ'><span id='BlfUQ'><b id='BlfUQ'><form id='BlfUQ'><ins id='BlfUQ'></ins><ul id='BlfUQ'></ul><sub id='BlfUQ'></sub></form><legend id='BlfUQ'></legend><bdo id='BlfUQ'><pre id='BlfUQ'><center id='BlfUQ'></center></pre></bdo></b><th id='BlfUQ'></th></span></q></dt></tr></i><div id='BlfUQ'><tfoot id='BlfUQ'></tfoot><dl id='BlfUQ'><fieldset id='BlfUQ'></fieldset></dl></div>