emberjs通过搜索加载模型数据

时间:2015-04-20 17:10:42

标签: ember-cli

我为搜索用户创建了一条路线,application.hbs正在搜索表单,下面是我的代码段。

ROUTER router.js

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

export default Router.map(function() {
  this.resource('search', {path: '/search/:citySef/:group'});
});

路线 application.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return Ember.RSVP.hash({
      groups: this.store.find('group'),
      cities: this.store.find('city', {type: 'filled'})
    });
  },
  setupController: function(controller, models) {
    controller.setProperties(models);
  }
});

路线 search.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params){
    return Ember.RSVP.hash({
      users: this.store.find('user', {citySef: params.citySef, group: params.group})
    });
  },
  setupController: function(controller, models) {
    controller.setProperties(models);
  }
});

控制器 application.js

import Ember from 'ember';

export default Ember.Controller.extend({
  isProcessing: false,
  selectedCity: null,
  selectedGroup: null,

  actions: {
    submitSearch: function () {
      this.setProperties({
        isProcessing: true
      });

      var citySef = this.get("selectedCity");
      var group = this.get("selectedGroup");

      console.log(citySef, group);
      this.transitionToRoute('search', {citySef:citySef, group:group});
    }
  }
});

控制器 search.js

import Ember from 'ember';

export default Ember.Controller.extend({
});

所以基本上我正在创建一个像localhost:4200/search/City/Group这样的网址来搜索具有特定群组的城市内的用户。

当我按下搜索按钮时,我可以看到地址栏中的URL已从localhost:4200更改为localhost:4200/search/City/Group但它不会向服务器发送请求以从REST获取数据,但是当我刷新页面,然后加载数据。

修改

除了模板之外,我已经包含了很多东西。

修改 2015-04-22

我已将软件包(没有bower和npm软件包)上传到https://drive.google.com/file/d/0B5slzmNBINvNMHNjcXdqNWlCeUk/view?usp=sharing

2 个答案:

答案 0 :(得分:0)

我猜你的案例中的问题是第一次没有正确设置模型。

首先,当你的路线中有模型功能时,你不必显式调用setupController函数,因为模型钩子处理所有这些设置。 但如果你愿意,那么惯例应该是 -

setupController:function(controller,models){
        controller.set('content',models);
   }

尝试使用上述模式中的设置控制器功能。

答案 1 :(得分:0)

根据Neha的建议,它与setupController

有关
 setupController: function (controller, models) {
    if (models.citySef) {
      controller.set('model', this.store.find('user', {citySef: models.citySef, group: models.group}));
    } else {
      controller.set('content', models);
    }

  }

因此,当页面加载时,它会转到其他条件,如果我转换它,它会进入条件。我认为这可以清理,但到目前为止它对我有用。