Question

I was wondering if interacting between emberViews is possible ??

Using controllers i had something like this set up that worked.

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

And In StudyIndex HBS i used this, which was handled now in the controller between needs tags

{{action 'deleteStudy' this target='controller.controllers.study/study'}}
Was it helpful?

Solution

This is not directly possible between views. Instead you should implement an indirect communication with the help of your controllers. Consider the following fictious example of two views and their associated controllers:

You should send an action from FirstView to FirstController (which needs SecondController). The Controller then forwards the action to SecondController. SecondController does whatever it needs to do (e.g. sets a property) and thus informs SecondView about it.

Update: Example

Plz note: I am assuming that you need to send to an object from FirstView to the SecondView. If you don't need an argument for your event, you can just omit it.

View:

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

OTHER TIPS

Interacting between Ember views is certainly possible but it depends on what you mean by 'interacting.' If you mean accessing properties, actions, and functions in other views then this can be done easily in a couple of different ways.

Firstly, you could extend one view from another:

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

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

Secondly, you could do this using a mixin. For example:

App.BaseStuff = Em.Mixin.create({
    // Functions and other logic here
});

App.OtherView = Em.View.extend(
    App.BaseStuff, { // Add mixin here
    // View logic here
});

However, if you are asking whether one view can access the content or model available to another view then this is not possible without further work. For example, using the needs property in the controller or accessing the data in your current route, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top