javascript - How to attach an event listner to an object in JS -


It seems as if people are doing it. I am not able to understand enough of my codebase, although similar patterns work to do.

Effectively I have an MVVM style application with UI based on the jQuery tab. Each tab is represented by a visual model which I validate and fire the event based on changes in the model Want to

I represent the page load on my data similar to the following:

  $ (document) .ready (function () {thisTab = newTitT ();}) ; Save this tab (Load: {load from my model}: Save: {save dB / durability model (via web service call)} Validate: {this.Item1 = function () {Validate item 1 , Do the field, refresh fields, whatever.}}}   

The model is a complex global object and updates the changes in the dom (input, etc.) Change some properties from their associated valid items to thisTab.Validate.Item1 There is no issue of increasing incidents from change to me. If I bind the event listener to a random dom element, then I can call my routine without any problem and everything works beautifully. It seems odd to attach the event to a non-related DOM object.

The question is then: can something like: thisTab.addEventListner ("someEvent") < / Code> or $ (thisTab) .bind ("s OmeEvent ") , where this tab is not a DOM element, but instead of trying to do this as a native object, I always get an error that" this method is not supported ".

The event does not use the same methods for a standard object; Basically, you will implement your own event like this:

  function thisTab () {listeners: [], adlister: function (callback) {this.listeners.push (callback) ; }, Load: {// Finds DOM elements and adds, and their event; Callback from the Dom event should be a method on your object}, your dot command callback: function () {for (var j = 0; J & lt; this.listeners.length; j ++) this.listeners [j] (); }}   

The above code should be used as a starting point, because I pulled it together and possibly syntax errors, in fact, you have taken your object and that event Mapped to what you want to capture, and then expose ways to engage callback methods that you will call when there are hidden dom events. You will not be able to use jQuery Separation for DOM events, because such incidents have no meaning on your custom object.

Comments