基于控制器的动作,在Ember中更改视图的正确方法是什么?

时间:2014-01-31 21:21:57

标签: ember.js

我目前在模板中设置了一个操作,其目的是跟踪用户的选择,然后根据该选择更改页面。

这是我router的适用部分:

this.resource('simpleSearch', function() {
    this.resource('simpleSearchOption', {path: ':simpleSearchOption_id'});

以下是行动:

<div {{action "select" this}} class="questiontile">

这是控制器:

App.SimpleSearchOptionController = Ember.ObjectController.extend({

    needs: ["simpleSearch"],
    simpleSearch: Ember.computed.alias("controllers.simpleSearch"),

    actions: {
        select: function(optionId) {
            var nextOptionId = parseInt(this.get("id")) + 1;
            var numOfOptions = this.get('simpleSearch').get('model').length;
            if(nextOptionId < numOfOptions) {
                console.log('going to next option');

                  /** What do I do here?
                   *  This is my current implementation, 
                   *  and it works, but is it proper?   
                   */
                  this.transitionToRoute('/simpleSearch/' + nextOptionId);

            }
        }
    }
});

下一页基本上是一个对象数组的下一个索引,它是父路径/控制器/视图的模型。

我现在如何做到这一点正在发挥作用 - 但这是正确的吗?它是'Ember idiomatic'吗?

1 个答案:

答案 0 :(得分:1)

对于以前的帖子抱歉,不小心将其删除了! transitionToRoute有两个参数,第一个是资源/路由名,第二个是模型。所以这应该有用

actions: {
    select: function(optionId) {
        var nextOptionId = parseInt(this.get("id")) + 1;
        this.store.find('simpleSearch', nextOptionId).then(function(model){
              this.transitionToRoute('simpleSearchOption', model); 
        });
        //OR MAYBE YOU COULD GET IT FROM THE PARENT CONTROLLER??
      /*
       MAYBE
       this.get('simpleSearch.content').forEach(function(model){
             if(model.get('id') === nextOptionId){ do transition}
              else{ alert some msg!! }
       })
      */
    }
 }

此处有更多信息:http://emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute