余烬在视图之间使用 [需求]



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

使用控制器,我有这样的东西可以工作。

>> 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中,我使用了这个,现在在需求标签之间的控制器中处理

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

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

您应该将操作从 FirstView 发送到 FirstController(需要 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
    }
});

控制器:

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);
    }
});

在 Ember 视图之间进行交互当然是可能的,但这取决于您所说的"交互"是什么意思。如果您的意思是访问其他视图中的属性、操作和函数,那么这可以通过几种不同的方式轻松完成。

首先,您可以从一个视图扩展另一个视图:

App.BaseView = Em.View.extend({
    // View logic here
});
App.OtherView = App.BaseView.extend({
    // Other view logic here
});

其次,您可以使用mixin来做到这一点。例如:

App.BaseStuff = Em.Mixin.create({
    // Functions and other logic here
});
App.OtherView = Em.View.extend(
    App.BaseStuff, { // Add mixin here
    // View logic here
});

但是,如果您询问一个视图是否可以访问另一个视图可用的内容或模型,那么如果没有进一步的工作,这是不可能的。例如,在控制器中使用 needs(英语) 属性或访问当前路由中的数据等。

最新更新