Ember.js:使用{{action}}时,如何定位特定的控制器?

时间:2013-06-03 14:55:32

标签: ember.js

我在这里的代码示例中提出了我的问题:

http://jsbin.com/urukil/12/edit

请参阅,我可以使用{{action}}(放置在子视图中)target选项来触发ApplicationViewApplicationController或{{1}中的事件除了ChildView之外,只有我真正想要的那个。

根据该文档,如果没有指定ChildController,则事件本身应该在相应的控制器中处理,在我的情况下,应该是target。但是为什么这个动作总是在ChildController中查找?我错过了一些显然很重要的东西吗?

4 个答案:

答案 0 :(得分:18)

您可以使用needs在不同的控制器上调用操作...

 App.ApplicationController = Ember.Controller.extend({
  needs: ['child'],
  doSomething: function() {
  alert("From ApplicationController");
  }
});

目标可以从模板

指定为“controllers.child”
<p {{action doSomething target="controllers.child"}}>Blah blah</p>

这是你的工作小提琴......

http://jsbin.com/agusen/1/edit

答案 1 :(得分:7)

使用this.controllerFor('')调用不同的控制器事件。下面给出一个工作实例。

JS:

/// <reference path="Lib/ember.js" />
var app = Ember.Application.create()
app.Router.map(function () {
    this.resource('post')

});
app.ApplicationRoute = Ember.Route.extend({

    model: function () { 
        return { "firstName": "amit", "lastName": "pandey" }
    }
});
app.ApplicationController = Ember.ObjectController.extend({
    Address: "House no 93-B",
    fullName: function () {
        return this.get("model.firstName") + " " + this.get("model.lastName")
    }.property("model.firstName", "model.lastName"),
    actions: {
        submit: function (name) {

            this.controllerFor('post').send('handleclick')

        },
        makeMeUpper:function()
        {
            alert('calling application controller Event');
           this.set("model.firstName",this.get("model.firstName").toUpperCase())
        }


    }



});


app.PostRoute = Ember.Route.extend({
    model:function()
    {
        return user;


    }


});
app.PostController = Ember.ObjectController.extend({
    Hello: "afa",
    handleclick: function ()
    {
        alert('calling post controller Event');

        this.controllerFor('application').send('makeMeUpper');
    }


});


var user = [
    {
        id: "1",
        Name: "sushil "

    },
    {
        id: "2",
        Name: "amit"

    }

];

// HBS

 <script type="text/x-handlebars">
      <button {{action submit firstName}}>CLICK HERE TO CALL Post controller event</button>
       {{input type="text" action= "makeMeUpper" value=firstName }}     

       {{#if check}}
      No Record Exist

       {{else}}
       {{firstName}}{{lastName}}
       {{/if}}
       {{#linkTo 'post'}}click {{/linkTo}}
       {{outlet}}
   </script>
        <script type="text/x-handlebars" id="post">

            <button {{action hanleclick}}>Click here to call application controller event</button>
        </script>

答案 2 :(得分:0)

据我所知,视图类不会更改当前控制器。由于您从Application模板调用视图,因此它仍保留在ApplicationController中。

Emberjs.com关于渲染的指南:

{{render}} does several things:

When no model is provided it gets the singleton instance of the corresponding controller

简单地将代码从视图更改为渲染调用似乎可以解决问题:

Trigger ApplicationController
</p>
{{render 'child'}}

答案 3 :(得分:0)

由于不推荐使用controllerFor,现在正确的方法是在控制器中指定需求,从控制器列表中检索它,然后将其发送到那里。例如:

App.SomeController = Em.Controller.extend({
  needs: ['other'],
  actions: {
    sayHello: function () {
    console.log("Hello from inside SomeController.");
    this.get('controllers.other').send('helloAgain');
    }
  }
});

App.OtherController = Em.Controller.extend({
  actions: {
    helloAgain: function () {
      console.log("Hello again from inside OtherController!");
    }
  }

});

编辑:oops ...看起来有人已经在本质上发布了这个答案。如果需要,会修改。