簡體   English   中英

Ember.js中ArrayController的重點是什么?

[英]What's the point of ArrayController in Ember.js?

該文檔有一個使用ArrayController與此模板的示例:

{{#each MyApp.listController}}
  {{firstName}} {{lastName}}
{{/each}}

這是ArrayController的使用方式:

MyApp.listController = Ember.ArrayController.create();

$.get('people.json', function(data) {
  MyApp.listController.set('content', data);
});

這比使用像這樣的普通數組有何不同?

MyApp.listController = [];

$.get('people.json', function(data) {
  MyApp.set('listController', data);
});

如果您不需要控制器的行為,則可以使用普通數組。

ArrayController包裝一個數組,並添加了一些其他屬性,例如可排序的mixin。 你可以在這里看到它:

https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/controllers/array_controller.js

在ember.js文檔中說:

(http://docs.emberjs.com/symbols/Ember.ArrayController.html)

使用ArrayController的優點是你只需要設置一次視圖綁定; 要更改顯示的內容,只需在控制器上交換內容屬性即可。

它在后台使用一個數組,只對使用數組的方法有所幫助:

雖然您綁定到控制器,但此控制器的行為是將任何方法或屬性傳遞給基礎數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM