Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : How to remove controllers without deleting the parent Element?

$
0
0
another way is (thats what i´ve done) to first add a wrapper inside that element you want to attach a Controller:
  1. <div id="widgetbox"></div>
then..
  1. // add new element inside
  2. $('#widgetbox').append('<div class="Controller"></div>');

  3. // attach the controller to this element
  4. var weatherWidget = new WeatherWidget("#widgetbox .Controller",{});

so everytime i want to remove a instance from the dom, i just can call the instance´s element remove method
  1. weatherWidget.element.remove();
and the dom is cleaned and events removed. job done.

It´s a bit dirty because you have to add a new dom node yourself every time, but it works. i´m curious that the docs dont say a word about how to glue everything (or complex apps) together. 

Backbone address this with Views (they have no Controllers) that dont attach themselves to a dom element, it creates a new one in memory instead.
  1. var widget = new WeatherWidget(); // Backone.View 
  2. widget.render() // process templates n stuff
  3. $('#widgetbox').append( widget.el ); // append the dom element or process it further
  4. widget.remove(); // dom & event cleanup
you end up with the same html structure you started.

Viewing all articles
Browse latest Browse all 3491

Trending Articles