繁体   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