2013-04-18 37 views
0

我可以绑定到模板like this工作:灰烬结合模板工程,但不通过延长

{{view App.MyView fooBinding:author}} 

但是,当我做同样的extendlike this

App.MyView = Ember.View.extend 
    fooBinding: "App.ApplicationController.author" 

它不”工作。

我错过了什么?

+0

为什么你不能使用第一种方法?你不能像这样引用'ApplicationController',因为这是类定义,而不是实例。 – CraigTeegarden

+0

我只是在修补Ember以了解它是如何工作的。只是好奇:ApplicationController实例的路径是什么? – treydavis

回答

0

如果你想绑定在View类中,你应该使用Controller

根据命名约定,名为AuthorView的视图将查找AuthorController或自动生成基本控制器。在你的代码中,你正在设置一个绑定到类(代表类型)而不是实例(这是你的具体对象)。

如果你创建一个控制器,你可以在那里做定义author财产,并在您的视图类,你可以因为它知道controllerAuthorController实例定义绑定,如fooBinding: "controller.author"

App.AuthorController = Ember.Controller.extend 
    author: Author.create 
     firstName: 'Bill' 
     lastName: 'Buckley' 

App.AuthorView = Ember.View.extend 
    templateName: 'author' 
    fooBinding: "controller.author" 
    fullName: (-> 
      author = @get 'foo' 
      "#{author.get('firstName')} #{author.get('lastName')}" 
    ).property 'firstName', 'lastName' 

这种方法会强迫你使用view.binding.property在车把模板:

<script type="text/x-handlebars" data-template-name='author'> 
    Written by {{view.fullName}} 
    <br /> 
    From Controller binding "fooBinding": {{view.foo.firstName}} {{view.foo.lastName}} 
</script> 

(见fiddle

另一种方式来做到这一点,是从路由设置author属性通过Route#setupController

Author = Ember.Object.extend 
    firstName: null 
    lastName: null  
    fullName: (-> 
     author = @get 'foo' 
     "#{@.get('firstName')} #{@.get('lastName')}" 
    ).property 'firstName', 'lastName' 

App.ApplicationRoute = Ember.Route.extend 
    setupController: (controller, model) -> 
     controller.set 'author', Author.create 
      firstName: 'Bill', 
      lastName: 'Buckley' 

和你的模板可以直接访问author属性,因为它是在该视图的控制器:

<script type="text/x-handlebars" data-template-name='author'> 
    written by: {{author.fullName}} 
</script> 

(见fiddle

这样,您就不必设置任何地方任何约束力。

注: 在你的对象创建计算的特性,因为它可能被用来不仅由视图,但是从中可以使用作者实例,并避免与undefined初始化属性的其他对象。


为了更好地使用Ember的功能,您可以定义为作者的路线,并与您Author实例设置控制器content财产,并添加一个{{outlet}}到您的模板。该框架会发现你的控制器和连接模板,再次使用命名约定:

把手

<script type="text/x-handlebars" data-template-name='author'> 
    written by: {{fullName}} 
</script> 

<script type="text/x-handlebars"> 
    {{outlet}} 
</script> 

咖啡

window.App = App = Ember.Application.create() 

App.Router.map -> 
    @.route 'author', { path: '/'} 

App.Author = Ember.Object.extend 
    firstName: null 
    lastName: null  
    fullName: (-> 
     "#{@.get('firstName')} #{@.get('lastName')}" 
    ).property 'firstName', 'lastName' 

App.AuthorRoute = Ember.Route.extend 
    setupController: (controller, model) -> 
      # this could come from an api 
      controller.set 'content', App.Author.create 
       firstName: 'Bill', 
       lastName: 'Buckley' 

App.AuthorController = Ember.ObjectController.extend() 

(见fiddle

+0

更改绑定到“controller.author”使我原来的例子工作。如果没有AuthorController,AuthorView的控制器就是ApplicationController实例。 [小提琴](http://jsfiddle.net/vr2E5/5/) – treydavis

+0

是的。发生这种情况是因为应用程序控制器持有'author'对象,但是如果您移动到其他路径,那么可能无法使用相同的路径('controller.author')。我知道你的示例不使用路由,但是一旦你的应用开始增长,你很可能会使用它,这就是为什么我使用作者的路由进行示例。 – MilkyWayJoe