10

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

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

看,我可以使用{{action}}带有选项的 a (放置在子视图中)在or或target中触发事件,只有我真正想要的除外。ApplicationViewApplicationControllerChildViewChildController

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

4

4 回答 4

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

于 2013-06-03T16:45:11.060 回答
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>
于 2013-09-17T18:17:05.437 回答
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'}}
于 2013-06-03T15:51:31.380 回答
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!");
    }
  }

});

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

于 2015-03-06T19:46:32.903 回答