Javascript MVC events

Models trigger (server) events: create, update and destroy. These can be listened to.
 But also attribute changes can be listened for (but only example I found is bind).

Model Lists trigger events: add, updated and remove. See "Listening to events on Lists" section.


The way to go is use templated binding as much as possible, i.e. pass in the objects/classes you wish to listen to. As Justin said: "..but using bind and such is typically not the way to do things anymore"

Justin said best way for controllers to trigger custom events is to use jquery trigger. Another option may be to create a model class which a controller may change and to which other controllers listen to such change events.


Jsmvc changing model eventing. Note: Justins comment " encouraging open ajax adds a lot of weight. I still wouldn't encourage using OpenAjax for server responses.  Server responses are almost always a result of a change in the model layer."

Fire events manually from Model 



This listener will fire when update() is called on any model instance

        MyProject.Models.PCat.bind('updated', function (ev, item) {
          console.log("a PCAT changed someplace");
        });  



...done as templated (note: must pass in PCat as a param to the controller at creation time)


    "{PCat} updated" : function(PCat, ev, newPCat) {
        console.log("Updated a PCat!");
    },

Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more