Javascript 在视图之间使用[需要]的余烬

Javascript 在视图之间使用[需要]的余烬,javascript,view,ember.js,action,Javascript,View,Ember.js,Action,我想知道是否可以在视图之间进行交互 使用控制器,我做了类似的设置,效果很好 >> Index Controller var StudyController = Ember.ArrayController.extend({ needs: ['study/study'], actions: { filterStudies: function(){ console.log('FILTER ENGAGED!');

我想知道是否可以在视图之间进行交互

使用控制器,我做了类似的设置,效果很好

>> 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。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);
    }
});
控制器:

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

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

您应该将操作从FirstView发送到FirstController(需要SecondController)。然后,控制器将操作转发给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);
    }
});
控制器:

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

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

您应该将操作从FirstView发送到FirstController(需要SecondController)。然后,控制器将操作转发给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);
    }
});
控制器:

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

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

您应该将操作从FirstView发送到FirstController(需要SecondController)。然后,控制器将操作转发给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);
    }
});
控制器:

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

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

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

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

但是,如果您询问一个视图是否可以访问另一个视图可用的内容或模型,则在没有进一步工作的情况下,这是不可能的。例如,使用控制器中的“需要”属性或访问当前路线中的数据等。

当然可以在余烬视图之间进行交互,但这取决于“交互”的含义。如果您是指访问属性、操作,和其他视图中的函数,那么这可以通过两种不同的方式轻松完成

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

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

但是,如果您询问一个视图是否可以访问另一个视图可用的内容或模型,则在没有进一步工作的情况下,这是不可能的。例如,使用控制器中的“需要”属性或访问当前路线中的数据等。

当然可以在余烬视图之间进行交互,但这取决于“交互”的含义。如果您是指访问属性、操作,和其他视图中的函数,那么这可以通过两种不同的方式轻松完成

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

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

但是,如果您询问一个视图是否可以访问另一个视图可用的内容或模型,则在没有进一步工作的情况下,这是不可能的。例如,使用控制器中的“需要”属性或访问当前路线中的数据等。

当然可以在余烬视图之间进行交互,但这取决于“交互”的含义。如果您是指访问属性、操作,和其他视图中的函数,那么这可以通过两种不同的方式轻松完成

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

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
});
但是,如果您询问一个视图是否可以访问另一个视图可用的内容或模型,则在没有进一步工作的情况下,这是不可能的。例如,使用控制器中的“需要”属性或访问控制器中的数据