Hi there,
I have a short question. Perhaps I should first describe my
use-case. In my spare-time I am trying to program something like a
miniature LinkedIn where I can maintain my work experience and
education data. The next step would be to connect the LinkedIn API and
try to get my data from the platform.
I have an overview of the currently maintained entries at the
top and when I want to edit an entry I will hit an edit button. This
will show the item (which should be edited) also under the table in
a form prefilled with all the entries (like start date, degree,
institution etc.). So the main problem is that an item (which has a
binding on the change event) will be shown twice (in the overview
table and in the maintain form). So when I change an entry in the
editable form the overview table will automatically change which I
think will confuse the user. My first attempt was to copy the item
which should be edited but unfortunately this didn't worked for me.
So I wanna ask is there something like a pause for an event
listenting until the user confirmed or cancelled the editing of the
entry and then the event listening will be resumed. So the edited item
should become 'deaf' for a defined time such that a change in the
maintain form will not automatically change the overview table.
I found an old thread here,
but I guess this does not apply for canJS? link:
https://forum.javascriptmvc.com/topic/pause-able-resume-able-events
I tried to program a workaround with the following two
functions. The first function will save the currently attached/bound
events for a given eventName and then unbinds all events from a
given item => pause event listening (item will become 'deaf' for
a specific event). The second function will then rebind the saved
event handlers to the given item => resume event listening
Here are the functions. Is there any more elegant way to do
this? I think the main problem is that I read some data from
internal properties e.g. like __bindEvents which could be renamed or
dropped in future releases and hence would lead to problems and
incompatibility of the code.
- ...
- // unbind an event with the given eventName from the given item
- // the eventHandler for the event with the eventName will be temporary
-
// stored in 'this.tmpChangeEventHandler'
- _unbindAndSaveEventHandler: function(eventName, item) {
- // get the event handler from the item
- var eventHandler = item.__bindEvents[eventName];
-
- for(var key in eventHandler) {
-
if(isNaN(key)) {
- // only store the real event handler
-
continue;
- }
- this.tmpChangeEventHandler.push(eventHandler[key].handler);
- }
-
// after we saved the event handler => let's unbind the event
-
// this will make the item 'deaf' for those event
-
// until we rebind the handlers back to the item
- item.unbind(eventName);
-
},
-
-
// rebind the temporary stored event handler to the event with the
- // given eventName to the given item
-
_rebindEventHandler: function(eventName, item) {
- // let's rebind the stored event handler to the item
-
if(this.tmpChangeEventHandler.length > 0)
- {
- for(var key in this.tmpChangeEventHandler) {
-
item.bind(eventName, this.tmpChangeEventHandler[key]);
-
}
- }
-
// reset the array with all the saved event handler
- this.tmpChangeEventHandler = new Array();
- },
- ...
Best Regards and thanks in advance
Patrick