javascript - Ember 在 View 之间使用 [needs]

标签 javascript view ember.js action

我想知道 emberViews 之间是否可以交互?

使用 Controller ,我进行了类似的设置,效果很好。

>> Index Controller
    var StudyController = Ember.ArrayController.extend({
      needs: ['study/study'],
      actions: {
        filterStudies: function(){
          console.log('FILTER ENGAGED!');
          this.transitionToRoute('study.search', this.get('search'));
        }
      }
    });

在 StudyIndex HBS 中我使用了这个,它现在在需求标签之间的 Controller 中处理

{{action 'deleteStudy' this target='controller.controllers.study/study'}}

最佳答案

这在 View 之间不能直接实现。 相反,您应该在 Controller 的帮助下实现间接通信。请考虑以下两个 View 及其关联 Controller 的虚构示例:

您应该将操作从 FirstView 发送到 FirstController(需要 SecondController)。然后 Controller 将操作转发给 SecondController。 SecondController 执行它需要执行的任何操作(例如设置属性),从而通知 SecondView。

更新:示例

请注意:我假设您需要将一个对象从 FirstView 发送到 SecondView。如果您的事件不需要参数,则可以省略它。

查看:

App.FirstView = Ember.View.extend({
    submit : function(){
            //do the view part of your logic
        var object = //do whatever you may need
        this.get("controller").send("actionInController", object); //you do not have to send a object, if you do not need to
    }
});

Controller :

App.FirstController = Em.ObjectController.extend({
    needs : ['second'],
    actions : {
        submitInController: function(object) {
          // do the controller part of your logic
          this.get("controllers.second").methodOfSecond(object);
        }
    },
});  

App.SecondController = Em.ObjectController.extend({
    someProperty : null,
    methodOfSecond: function(object) {
        // set whatever property so that the second view gets informed
        this.set("someProperty", object);
    }
});

关于javascript - Ember 在 View 之间使用 [needs],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174853/

相关文章:

php - 选中复选框后,我想通过 jquery 将其值发送到其他 PHP 文件

javascript - 使用 Nodejs 的 Moment.js Sql 日期时间转换

objective-c - 从现有 Controller 创建 UIPopoverController

mysql - 在具体化子查询上指定外部查询的条件

javascript - 悬停时的旋转按钮

javascript - 如何使用 javascript 在单击时将 html 表单字段值复制到另一个字段?

iphone - 如何在 iOS 中重新加载我的 View

javascript - Ember 2,性能困境,路由或 Controller 中的计算属性?

javascript - Object.values 在 IE 浏览器中抛出 TypeError - 如何填充它?

javascript - Ember JS : Issue with Query Params for a Route and Backward Navigation