fire an event on the view side

18 views
Skip to first unread message

mauro larese

unread,
Mar 22, 2014, 2:55:04 PM3/22/14
to gl...@googlegroups.com
Is it possible from a model to fire an event that is observed on the view side ... o in other words is it possible to listen on the view to events fired in the model ... thanks in advance
- Mauro

Ryan Smith

unread,
Mar 22, 2014, 2:59:26 PM3/22/14
to gl...@googlegroups.com

I've had that thought a few times and then I was forced to remember that I shouldn't be doing that because it breaks the MVVM pattern and makes it impossible to unit test properly.  To do something like that you can add a property to your viewmodel and implement a set property function which fires the event on the view.  It feels a little hacky because it is... But it works :).  Hope that helps!

Ryan

On Mar 22, 2014 11:55 AM, "mauro larese" <mauro....@gmail.com> wrote:
Is it possible from a model to fire an event that is observed on the view side ... o in other words is it possible to listen on the view to events fired in the model ... thanks in advance
- Mauro

--
You received this message because you are subscribed to the Google Groups "GluJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glujs+un...@googlegroups.com.
To post to this group, send email to gl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glujs/10665b9e-e5cb-44b2-a33b-5e26327e29f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mauro larese

unread,
Mar 22, 2014, 4:40:39 PM3/22/14
to gl...@googlegroups.com

Michael Gai

unread,
Mar 22, 2014, 5:11:54 PM3/22/14
to gl...@googlegroups.com
Hi Mauro:

Another way is to use the (I'm pretty sure completely undocumented :) 'glubind' event along with the view's _vm reference. I have run into a few cases where it really was just a "one-off" event firing that my view needed to something about, rather than a state/property change. For instance, sometimes a "close" event is easier to model this way than as an "isOpen" boolean property.

The glubind event fires on the view after GluJS is finished binding - so there should be a _vm reference to attach to from the view. If not there (it depends on the version of glu), the view model will also be at view._bindings.defaultModel.

Here's a snippet:
//..
listeners
:{
      glubind
:function(view){
            view
._vm.on('closepopup',function(){
               
//do the custom close...
           
});
       
}
}//...

Hope that helps and gives you another option.

Mike

mauro larese

unread,
Mar 23, 2014, 6:19:33 AM3/23/14
to gl...@googlegroups.com
thank you very much .. 
two options .... 
perfect.
now i will make my choice. Anyway i've tried the one suggested by Ryan and it works pretty well.

- Mauro


mauro larese

unread,
Mar 23, 2014, 6:34:03 AM3/23/14
to gl...@googlegroups.com
i prefer rayan solution because there is no any relationship between view and model. In Michael trick ;-) there is a tight relationship with the _vm object (dependence between view and model) and _vm  is private.
I hope this can be helpful for the forum.

-Mauro (Again ...   :-D  )
Reply all
Reply to author
Forward
0 new messages