2

我正在使用 emebr-1.0.pre.js

对每个助手上下文有疑问,下面的代码供参考。在输出 UI 标题中替换“book1”和“book2”,但输入字段为空。

我的疑问是每个助手中使用了哪个上下文,在 {{each}} 助手中“this”指的是什么?

window.App = Ember.Application.create({});


App.Book = Ember.Object.extend({
   title : null,
  author : null
 });

BooksController.js

App.BooksController = Ember.ObjectController.extend({
    content : [
                App.Book.create({title : "Book1", author : 'author1'}),
                App.Book.create({title: 'Book2', author : 'author2'})
               ]           
});

BooksListView.js

 App.BooksView = Ember.View.extend({
  templateName : "books_list",
  controllerBinding : "App.BooksController"
})

车把

{{#each content}}
   <label>{{this.title}}</label>
   {{view Ember.TextField valueBinding="this.author"}}
{{/each}}

JSFiddle在这里

4

1 回答 1

1

首先,BooksListView有一controllerBinding组 to App.BooksController

它应该是类似的东西App.booksController,因为你想将它绑定到一个实例而不是一个类:

App.booksController = App.BooksController.create();

App.BooksListView = Ember.View.extend({
   controllerBinding: "App.booksController"
});

About the {{each}} context, this refer to the current object, so this work as expected. Except that you don't should use {{this.aproperty}} on the {{each}} helper, just write {{aproperty}}:

{{#each content}}
  <label>{{title}}</label>
  {{view Ember.TextField valueBinding="author"}}
{{/each}}

And here is the JSFiddle which works. I have intentionally added {{this}} in order to let you see on which object it refers.

于 2012-10-04T10:18:35.530 回答