简体   繁体   English

Ember的房产是什么? 或者他们是方法吗?

[英]What are properties in Ember? Or are they methods?

This is probably a legitimately stupid question, but I feel like it is keeping me from understanding large portions of Ember. 这可能是一个合法的愚蠢问题,但我觉得这让我无法理解Ember的大部分内容。

In the following code: 在以下代码中:

App.IndexRoute = Em.Route.extend({
  skipSidebar: true
});    

What is 'skipSidebar:'? 什么是'skipSidebar:'? What is it in regard to the javascript programming language and what is it in Ember? 关于javascript编程语言是什么,它在Ember中是什么?

Another example: 另一个例子:

App.AboutRoute = Ember.Route.extend({
  activate: function(){
    this.controllerFor('application').set('renderAboutSubNav', true);
  },
  deactivate: function(){
    this.controllerFor('application').set('renderAboutSubNav', false);
  }
});

What are 'activate:' and 'deactivate:'? 什么是'激活:'和'停用:'?

In the first example I used 'skipSidebar' to render a partial: 在第一个例子中,我使用'skipSidebar'来渲染部分:

{{#unless skipSidebar}}
   {{partial 'sidebar'}}
{{/unless}}

But I'm not really sure why I did that or what it is doing. 但我不确定为什么我这样做或者它在做什么。

Essentially I see these names of what look like methods inside of routes and controllers and I'm not sure where they are coming from. 基本上我看到这些名称看起来像路线和控制器内部的方法,我不知道它们来自哪里。 If someone could explain this to me like I'm a golden retriever that would be awesome. 如果有人能向我解释这一点,就好像我是一只非常棒的金毛猎犬。 When can I use them? 我什么时候可以使用它们? When should I use them? 我应该什么时候使用它们? How do I use them? 我该如何使用它们?

Since you commented that you are familiar with OO programming then this should be easy for you to understand. 既然您评论说您熟悉OO编程,那么您应该很容易理解。

What is 'skipSidebar:'? 什么是'skipSidebar:'? What is it in regard to the javascript programming language and what is it in Ember? 关于javascript编程语言是什么,它在Ember中是什么?

App.IndexRoute = Em.Route.extend({
  skipSidebar: true
});    

In ember everything extends eg is a subclass of Ember.Object , Ember.Object in turn is the basic class object (the fundation) which provides you all the needed mechanics to programm in OO style. 在ember中,一切都是扩展的,例如Ember.Object的子类, Ember.Object又是基本的类对象(基础),它为OO风格的程序提供了所有必需的机制。 So in the case of skipSidebar , this is a class property allocated onto your extended Ember.Route object which as I said is also a subclass of Ember.Object . 所以在skipSidebar的情况下,这是一个分配到扩展Ember.Route对象的类属性,正如我所说的那样,它也是Ember.Object的子类。 So now imagine you would extend App.IndexRoute : 所以现在想象你会扩展App.IndexRoute

App.MyFooRoute = App.IndexRoute.extend({
  someFunction: function() {
    this.get('skypSidebar'); // this would retrieve the correct property defined in App.IndexRoute
  }
});

What are 'activate:' and 'deactivate:'? 什么是'激活:'和'停用:'?

App.AboutRoute = Ember.Route.extend({
  activate: function(){
    this.controllerFor('application').set('renderAboutSubNav', true);
  },
  deactivate: function(){
    this.controllerFor('application').set('renderAboutSubNav', false);
  }
});

In this case a Ember.Route has additional functions on top of the basic functionality already provided by Ember.Object , in the case of a route this additional functions are mostly routing related. 在这种情况下, Ember.Route对已经提供的基本功能之上的附加功能Ember.Object ,在的情况下, route这个附加功能大多路由相关。 To make developers live easier ember provides a so called public API which consists of stub functions one can override to be notified when this functions are called in it's lifecycle. 为了让开发人员更容易生成,ember提供了一个所谓的公共API,它包含一个可以覆盖的存根函数,以便在其生命周期中调用此函数时得到通知。 This functions are also called hooks . 这个函数也称为hooks In the case of activate and it's counterpart deactivate , this are called/invoked by ember when a route is about to become active, and respectively un-active eg when the route changes. activate和它的对应物deactivate的情况下,当路线即将变为活动时由ember调用/调用,并且例如在路线改变时分别不激活。

To take things further, imagine you want some basic functionality to be always executed but also you want to extend the class and override these hooks without loosing the basic logic, then there is a method for that called this._super() . 更进一步,想象一下你想要总是执行一些基本功能,但是你想要扩展类并覆盖这些钩子而不会丢失基本逻辑,那么有一个方法叫做this._super()

App.BasicRoute = Ember.Route.extend({
  activate: function(){
    alert('route activated');
  },
  deactivate: function(){
    alert('route deactivated');
  }
});

Now we extend App.BasicRoute : 现在我们扩展App.BasicRoute

App.DifferentRoute = App.BasicRoute.extend({
  activate: function(){
    this._super();
    // do stuff
  },
  deactivate: function(){
    this._super();
    // do atuff
  }
});

The above example would, call activate and respectively deactivate of it's parent class executing alert(...); 上面的例子将调用activate并分别deactivate它的父类执行alert(...); because we invoked this._super() in the sub class, and additionally execute whatever logic you might have defined in the sub class. 因为我们在子类中调用this._super() ,并且还执行您在子类中定义的任何逻辑。

Hope it helps. 希望能帮助到你。

From a pure Javascript syntax standpoint, this code: 从纯Javascript语法的角度来看,这段代码:

App.IndexRoute = Em.Route.extend({
  skipSidebar: true
});

is equivalent to this Ruby: 相当于这个Ruby:

App['IndexRoute'] = Em['Route'].extend( { 'skipSidebar' => true } );

That is, it's assigning an element of a Hash (which in Javascript all objects essentially are) to the result of a method call on an element of another Hash, with an argument that is yet a third Hash, this one in literal form (the basis of JSON). 也就是说,它将Hash的一个元素(在Javascript中所有对象本质上都是)分配给另一个Hash的元素上的方法调用的结果,其中一个参数是第三个Hash,这个是文字形式的( JSON的基础)。

In fact, you could write the JS in a form almost identical to the Ruby: 实际上,你可以用几乎与Ruby相同的形式编写JS:

App['IndexRoute'] = Em['Route'].extend( { skipSidebar: true } );

... since the Name.Key syntax in Javascript is just a convenient shortcut for Name['Key'] that works when the key string is a valid identifier. ...因为Javascript中的Name.Key语法只是Name['Key']一个方便的快捷方式,当密钥字符串是有效的标识符时,它可以工作。 This works, too: 这也有效:

App['IndexRoute'] = Em['Route']['extend']( { skipSidebar: true } );

because Javascript is like Python in that methods are really just attributes (Hash elements) whose value is a function/closure. 因为Javascript就像Python一样,方法实际上只是属性(Hash元素),其值是函数/闭包。

Semantically, however, what this is doing in Ember is defining a class called IndexRoute , in the App object (which is used for namespacing) as a subclass of the Ember-defined class Route (in the Em object/namespace), and adding a property skipSidebar that will default to true for all new App.IndexRoute objects. 然而,在语义上,这在Ember中所做的是在App对象(用于命名空间)中定义一个名为IndexRoute的类作为Ember定义的类Route的子类(在Em对象/命名空间中),并添加一个属性skipSidebar ,对于所有新的App.IndexRoute对象,默认为true So functionally, it's more like this Ruby: 所以在功能上,它更像是这个Ruby:

class App::IndexRoute < Em::Route
  attr_accessor :skipSidebar
  def initialize
    @skipSidebar = true
  end
end

In your second example: 在你的第二个例子中:

App.AboutRoute = Ember.Route.extend({
  activate: function(){
    this.controllerFor('application').set('renderAboutSubNav', true);
  },
  deactivate: function(){
    this.controllerFor('application').set('renderAboutSubNav', false);
  }
});

we're again defining a subclass (this time of Ember.Route rather than Em.Route ), and in the subclass adding or overriding two methods called activate and deactivate . 我们再次定义一个子类(这次是Ember.Route而不是Em.Route ),并在子类中添加或重写两个名为activatedeactivate方法。 Ruby: 红宝石:

class App::AboutRoute < Ember::Route
  def activate
    self.controllerFor('application').renderAboutSubNav = true
  end
  def deactivate
    self.controllerFor('application').renderAboutSubNav = false
  end
end

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

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