本文介绍了我是否必须为每个 Ember.js 应用程序显式创建一个“ApplicationView"和一个“ApplicationController"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Trek 的教程:

您的应用程序必须具有 ApplicationView 属性.一个实例将为您创建此类并将其插入到应用程序的视图层次结构作为根视图.

并且教程给出了这个示例代码:

And the tutorial gives this example code:

App.ApplicationView = Ember.View.extend({
  templateName: 'application'
});

App.ApplicationController = Ember.Controller.extend();

ApplicationViewApplicationController 有什么特别之处?它们的特定名称是保留的并且必须通过转换使用,还是路由器引用它们的方式有什么特别之处?

What is special about ApplicationView and ApplicationController? Is their particular name reserved and must be used by conversion or is there something special about the way the router refers to them?

据我所知,Ember 的原则之一是消除样板代码.所以我猜还有更多的东西可以为每个应用程序明确地创建这两个视图作为起点——否则 Ember 只会在幕后为我制作它们.

As I understand it, one of Ember's principals is to eliminate boilerplate code. So I am guessing there is something more to explicitly creating these two views for every app as starting point -- otherwise Ember would just make them for me behind the scene.

重新表述我的问题,是什么让 ApplicationViewApplicationController 在 Ember 应用程序中与众不同.我是否需要为每个应用明确创建它们,如果需要,我是否需要按照约定命名它们?

To rephrase my question, what makes ApplicationView and ApplicationController special in an Ember application. Do I need to create them explicitly for every app and if so, do I need to name them following a convention?

注意:我使用的是 ember-latest

Note: I am using ember-latest

推荐答案

Ember.js 试图坚持约定优于配置的编程哲学.因此,有些东西需要特别命名并遵循正确的大小写规则.在调用 App.initialize() 时,您的应用程序会在其自身 App.ApplicationView 上查找该属性.然后,您的应用呈现此视图,将其插入 dom 并自动创建 App.ApplicationController 的实例,将其设置为您的 ApplicationView 的呈现上下文.这意味着 ApplictationController 中的任何属性都可以在 ApplicationView 中绑定,只需在视图中引用它们即可.

Ember.js tries to adhere to the programming philosophy of convention over configuration. Because of this some things need to be specifically named and follow correct casing rules. Upon calling App.initialize() your application looks for the property on itself App.ApplicationView. Your app then renders this view, inserts it into the dom and auto-creates an instance of App.ApplicationController, setting it as the render context for your ApplicationView. This means that any properties in your ApplictationController can be bound in your ApplicationView simply by referencing them in the view.

如果你在没有 App.ApplicationViewApp.ApplicationController 的情况下调用 App.initialize() , ember 会抛出一个错误让你知道您必须创建它们.

If you call App.initialize() with out an App.ApplicationView or App.ApplicationController ember will throw an error letting you know you must create them.

ApplicationViewApplicationController 是 ember 应用程序的组成部分,必须存在.Ember 是一个 MVC 框架,ApplicationView 是你的根 V,ApplicationController 是你的根 C.尝试将像 ember 这样的框架用于你想要的部分可能很诱人,但只要多做一点工作,您就会拥有一个更强大、更易于使用的应用程序,该应用程序试图选择功能.

ApplicationView and ApplicationController are integral parts of your ember application and must exist. Ember is an MVC framework, ApplicationView is your root V, ApplicationController is your root C. It can be tempting to try to use frameworks like ember for just the pieces that you want, but with just a little more work you'll have a much more robust, and easy to use application that trying to cherry pick functionality.

这篇关于我是否必须为每个 Ember.js 应用程序显式创建一个“ApplicationView"和一个“ApplicationController"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:40