簡體   English   中英

ember js,如何從路由中使用控制器中的數組

[英]ember js, how can i use the array in the controllers from the routes

嗨,我是ember的新手,我想使用從服務器端獲取的數組中的某些數據進行處理。 但是我不知道該怎么做。 下面的代碼在路由中。

searchList: null,

model: function() {

  // This is so that we don't refresh it all the time
  // arguably, this is a hack.
  var searchList = this.get('searchList');

  if(searchList !== null) {
    return searchList;
  }

  var self = this;
  return Ember.$.ajax({
    url: 'http://coen268.peterbergstrom.com/timezones.php',
    dataType: 'jsonp'
  }).then(function(response) {
    var cities = [];  <-----------------------this is an array of names from server side
    if (response && response.length) {
      for(var i=0; i<response.length; i++) {
        cities.push(Ember.Object.create(response[i]));
      }
    }
    /*
    cities.sort(function(a, b) {
        return a.id - b.id
    })
    */
    self.set('searchList', cities);
    return cities;

我只想在控制器中使用city var,以便可以執行排序,添加和重新組織輸出到最終視圖html之類的操作。

非常感謝!

如果您的路線被調用,請說App.CitiesRoute ,那么您需要這樣聲明CitiesController:

App.CitiesController = Ember.ArrayController.extend({});

然后,您可以使用排序與簡單的控制性能,或過濾時調用filter()this (指你的ArrayController),在Ember的指南如圖所示,

您還可以訪問ArrayController的所有功能和屬性,如Ember API文檔中所示

您甚至可以通過在itemController中指定itemController屬性來為數組的每個項目指定特定的Controller,例如:

App.CitiesController = Ember.ArrayController.extend({
  itemController: 'city'
});

// Here you can specify an ObjectController linked to all your items in the cities array

App.CityController = Ember.ObjectController.extend({

  cityNameLowercase: function() {
    return this.get('cityName').toLowerCase();
  }.property('cityName')
});

它將綁定到陣列中的每個城市。

暫無
暫無

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

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